ScriptStack 1.0.4
Loading...
Searching...
No Matches
ScriptStack.Runtime.Script Class Reference

Internal representation of a text file (source code) which can be passed to the Interpreter to execute it. More...

Public Member Functions

 Script (Manager manager, string scriptName)
 Script (Manager manager, string scriptName, bool binary)
void CompileBinary (string fileName)
void CompileJSON (string fileName)
bool EntryPoint ()

Properties

Manager Manager [get]
string Name [get]
ReadOnlyCollection< String > SourceLines [get]
string Source [get]
Executable Executable [get]
Memory ScriptMemory [get]
ScriptStack.Collections.ReadOnlyDictionary< String, FunctionFunctions [get]
Function MainFunction [get]

Detailed Description

Internal representation of a text file (source code) which can be passed to the Interpreter to execute it.

Definition at line 17 of file Script.cs.

Constructor & Destructor Documentation

◆ Script() [1/2]

ScriptStack.Runtime.Script.Script ( Manager manager,
string scriptName )

Definition at line 219 of file Script.cs.

220 {
221
222 this.manager = manager;
223
224 this.scriptName = scriptName;
225
226 try
227 {
228
229 Scan(scriptName);
230
231 Lexer lexer = new Lexer(sourceCode);
232
233 List<Token> tokenStream = lexer.GetTokens();
234
235 Parser parser = new Parser(this, tokenStream);
236
237 parser.DebugMode = this.manager.Debug;
238
239 executable = parser.Parse();
240
241 if (this.manager.Optimize)
242 {
243
244 Optimizer optimizer = new Optimizer(executable);
245
246 optimizer.OptimizerInfo = false;
247
248 optimizer.Optimize();
249
250 }
251
252
253 }
254 catch (Exception exception)
255 {
256 throw new ScriptStackException("Fehler in '" + scriptName + "'.", exception);
257 }
258
259 }
List< Token > GetTokens()
Definition Lexer.cs:140
Executable Parse()
Parse the token stream into an executable.
Definition Parser.cs:3092

References ScriptStack.Manager.Debug, ScriptStack.Compiler.Lexer.GetTokens(), Manager, ScriptStack.Manager.Optimize, and ScriptStack.Compiler.Parser.Parse().

◆ Script() [2/2]

ScriptStack.Runtime.Script.Script ( Manager manager,
string scriptName,
bool binary )

Definition at line 261 of file Script.cs.

262 {
263
264 this.manager = manager;
265
266 this.scriptName = scriptName;
267
268 try
269 {
270
271 List<Token> tokenStream = null;
272 if (binary == true)
273 {
274 tokenStream = new List<Token>();
275 using (var fs = new FileStream(scriptName, FileMode.Open, FileAccess.Read))
276 using (var br = new BinaryReader(fs))
277 {
278 int count = br.ReadInt32();
279
280 for (int i = 0; i < count; i++)
281 {
282 var typeInt = br.ReadInt32();
283 var lexemeStr = br.ReadString();
284 var line = br.ReadInt32();
285 var column = br.ReadInt32();
286 var text = ""; // br.ReadString();
287
288 TokenType type = (TokenType)typeInt;
289 object lexeme = ConvertLexeme(type, lexemeStr);
290
291 tokenStream.Add(new Token(type, lexeme, line, column, text));
292 }
293 }
294 }
295 else
296 {
297 string json = File.ReadAllText(scriptName, Encoding.UTF8);
298
299 var serializableTokens = JsonSerializer.Deserialize<List<SerializableToken>>(json);
300
301 // Falls du wieder echte Token-Objekte brauchst:
302 tokenStream = serializableTokens
303 .Select(st => new Token(
304 st.Type,
305 ConvertLexeme(st), // (st.Lexeme) oder hier wieder in int/float/etc. umwandeln, wenn nötig
306 st.Line,
307 st.Column,
308 st.Text))
309 .ToList();
310 }
311
312 Parser parser = new Parser(this, tokenStream);
313
314 parser.DebugMode = this.manager.Debug;
315
316 executable = parser.Parse();
317
318
319 if (this.manager.Optimize)
320 {
321
322 Optimizer optimizer = new Optimizer(executable);
323
324 optimizer.OptimizerInfo = false;
325
326 optimizer.Optimize();
327
328 }
329
330
331 }
332 catch (Exception exception)
333 {
334 throw new ScriptStackException("Fehler in '" + scriptName + "'.", exception);
335 }
336
337 }
TokenType
Known types of Token.
Definition Token.cs:12

References ScriptStack.Compiler.SerializableToken.Column, ScriptStack.Manager.Debug, ScriptStack.Compiler.SerializableToken.Line, Manager, ScriptStack.Manager.Optimize, ScriptStack.Compiler.Parser.Parse(), ScriptStack.Compiler.SerializableToken.Text, and ScriptStack.Compiler.SerializableToken.Type.

Member Function Documentation

◆ CompileBinary()

void ScriptStack.Runtime.Script.CompileBinary ( string fileName)

Definition at line 339 of file Script.cs.

340 {
341
342 try
343 {
344
345 Scan(scriptName);
346
347 Lexer lexer = new Lexer(sourceCode);
348
349 List<Token> tokenStream = lexer.GetTokens();
350
351 using (var fs = new FileStream(fileName, FileMode.Create, FileAccess.Write))
352 using (var bw = new BinaryWriter(fs))
353 {
354 // Anzahl Tokens zuerst
355 bw.Write(tokenStream.Count);
356
357 foreach (var t in tokenStream)
358 {
359 bw.Write((int)t.Type); // Enum als int
360 bw.Write(t.Lexeme?.ToString() ?? ""); // als String
361 bw.Write(t.Line);
362 bw.Write(t.Column);
363 //bw.Write(t.Text ?? "");
364 }
365 }
366
367 }
368 catch (Exception exception)
369 {
370 throw new ScriptStackException("Fehler in '" + scriptName + "'.", exception);
371 }
372 }

References ScriptStack.Compiler.Lexer.GetTokens().

◆ CompileJSON()

void ScriptStack.Runtime.Script.CompileJSON ( string fileName)

Definition at line 374 of file Script.cs.

375 {
376
377 try
378 {
379
380 Scan(scriptName);
381
382 Lexer lexer = new Lexer(sourceCode);
383
384 List<Token> tokenStream = lexer.GetTokens();
385
386 var serializableTokens = tokenStream.Select(t => new SerializableToken
387 {
388 Type = t.Type,
389 Lexeme = t.Lexeme?.ToString(),
390 Line = t.Line,
391 Column = t.Column,
392 Text = t.Text
393 }).ToList();
394
395 var options = new JsonSerializerOptions
396 {
397 WriteIndented = true // für schön formatiertes JSON
398 };
399
400 string json = JsonSerializer.Serialize(serializableTokens, options);
401
402 // In Datei schreiben
403 File.WriteAllText(fileName, json, Encoding.UTF8);
404
405 }
406 catch (Exception exception)
407 {
408 throw new ScriptStackException("Fehler in '" + scriptName + "'.", exception);
409 }
410 }

References ScriptStack.Compiler.Lexer.GetTokens().

◆ EntryPoint()

bool ScriptStack.Runtime.Script.EntryPoint ( )

Definition at line 412 of file Script.cs.

413 {
414 return executable.FunctionExists("main");
415 }

Property Documentation

◆ Executable

Executable ScriptStack.Runtime.Script.Executable
get

Definition at line 450 of file Script.cs.

451 {
452 get { return executable; }
453 }

◆ Functions

ScriptStack.Collections.ReadOnlyDictionary<String, Function> ScriptStack.Runtime.Script.Functions
get

Definition at line 460 of file Script.cs.

461 {
462 get
463 {
464 return new ScriptStack.Collections.ReadOnlyDictionary<String,Function>(executable.Functions);
465 }
466 }

◆ MainFunction

Function ScriptStack.Runtime.Script.MainFunction
get

Definition at line 468 of file Script.cs.

469 {
470 get { return executable.MainFunction; }
471 }

◆ Manager

Manager ScriptStack.Runtime.Script.Manager
get

Definition at line 421 of file Script.cs.

422 {
423 get { return manager; }
424 }

Referenced by Script(), and Script().

◆ Name

string ScriptStack.Runtime.Script.Name
get

Definition at line 426 of file Script.cs.

427 {
428 get { return scriptName; }
429 }

◆ ScriptMemory

Memory ScriptStack.Runtime.Script.ScriptMemory
get

Definition at line 455 of file Script.cs.

456 {
457 get { return executable.ScriptMemory; }
458 }

◆ Source

string ScriptStack.Runtime.Script.Source
get

Definition at line 436 of file Script.cs.

437 {
438 get
439 {
440 StringBuilder sb = new StringBuilder();
441 foreach (string line in sourceCode)
442 {
443 sb.Append(line);
444 sb.Append("\r\n");
445 }
446 return sb.ToString();
447 }
448 }

◆ SourceLines

ReadOnlyCollection<String> ScriptStack.Runtime.Script.SourceLines
get

Definition at line 431 of file Script.cs.

432 {
433 get { return sourceCode.AsReadOnly(); }
434 }

The documentation for this class was generated from the following file: