Skip to content

Commit 2b08c58

Browse files
author
aafent
committed
new statement EVAL
1 parent 859c747 commit 2b08c58

8 files changed

Lines changed: 114 additions & 0 deletions

File tree

FAST.FBasic.InteractiveConsole/FBasicIC_Setup.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ private void setupEnvironment()
9090
env.AddLibrary(new FBasicJsonLibrary());
9191
env.AddLibrary(new FBasicTemplatingLibrary());
9292
env.AddLibrary(new FBasicAIChat());
93+
env.AddLibrary(new FBasicLanguageExtensions());
94+
9395
env.AddVariable("table.column", "myColumn1");
9496

9597
FBasicEvents.Reset();
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
REM Test program for EVAL statements
2+
REM Category: Flow Control
3+
REM
4+
RESULT 5
5+
6+
let code="
7+
print RESULTVALUE
8+
let X=10
9+
print X
10+
"
11+
12+
eval code
13+
let X=X+1
14+
print "X="+X
15+
16+

FAST.FBasic.LibraryToolkit/Core/InterpreterHelper.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public static bool IsStatement(Token token)
3535
case Token.Dump:
3636
case Token.Call:
3737
case Token.Chain:
38+
case Token.Eval:
3839
case Token.ForEach:
3940
case Token.EndForEach:
4041
case Token.SData:

FAST.FBasic.LibraryToolkit/Core/Token.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public enum Token
3030
Dump,
3131
Call,
3232
Chain,
33+
Eval,
3334
ForEach,
3435
EndForEach,
3536
SData,

FAST.FBasicInterpreter/Core/Interpreter_Elements.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ void Statement()
3131
case Token.Dump: DumpStatement(); break;
3232
case Token.Call: CallStatement(); break;
3333
case Token.Chain: ChainStatement(); break;
34+
case Token.Eval: EvalStatement(); break;
3435
case Token.ForEach: ForEachStatement(); break;
3536
case Token.EndForEach: EndForEachStatement(); break;
3637
case Token.SData: SDataStatement(); break;

FAST.FBasicInterpreter/Core/Interpreter_Statements.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,34 @@ void ChainStatement()
138138
return;
139139
}
140140

141+
/// <summary>
142+
/// EVAL
143+
/// </summary>
144+
void EvalStatement()
145+
{
146+
// (v) load the chain source program
147+
string srcCode = Expr().ToString();
148+
149+
// (v) save the current program state
150+
var oldState = this.GetState();
151+
152+
//// (v) Set the new chain program and execute it
153+
lex.SetSource(srcCode);
154+
exit = false;
155+
GetNextToken();
156+
while (!exit) Line(); // do all lines
157+
exit = false;
158+
159+
// (v) restore the old program state
160+
this.SetState(oldState);
161+
162+
// (v) continue with the next instruction
163+
164+
return;
165+
}
166+
167+
168+
141169
/// <summary>
142170
/// HALT | END
143171
/// </summary>

FAST.FBasicInterpreter/Core/Lexer.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ public Token GetToken()
168168
case "RETURN": return Token.Return;
169169
case "CALL": return Token.Call;
170170
case "CHAIN": return Token.Chain;
171+
case "EVAL": return Token.Eval;
171172
case "FOREACH": return Token.ForEach;
172173
case "ENDFOREACH": return Token.EndForEach;
173174

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
//
2+
// Language Extensions Statements:
3+
//
4+
//
5+
6+
using System.Text;
7+
8+
namespace FAST.FBasicInterpreter
9+
{
10+
public class FBasicLanguageExtensions : IFBasicLibraryWithMemory, IDisposable
11+
{
12+
private IInterpreter inter;
13+
14+
public string uniqueName => "FBasicLanguageExtensions";
15+
16+
public FBasicLanguageExtensions()
17+
{
18+
//IFBasicFileManagementLayer fileManager=new zzzzzzz();
19+
//this.fileManager=fileManager;
20+
}
21+
22+
public void InstallAll(IInterpreter interpreter)
23+
{
24+
this.inter = interpreter;
25+
26+
interpreter.AddStatement("EVAL", EVAL);
27+
28+
}
29+
30+
private static void EVAL(IInterpreter interpreter)
31+
{
32+
// Syntax: EVAL value|variable
33+
//
34+
string code = interpreter.ValueOrVariable(true).String;
35+
interpreter.GetNextToken();
36+
37+
38+
}
39+
40+
public void ClearMemory()
41+
{
42+
var variables = inter.GetVariables();
43+
foreach (var variable in variables)
44+
{
45+
if (variable.Value.Type != ValueType.Object) continue;
46+
47+
if (variable.Value.Object is Stream)
48+
{
49+
var stream = ((Stream)variable.Value.Object);
50+
stream.Flush();
51+
stream.Close();
52+
stream.Dispose();
53+
stream = null;
54+
}
55+
}
56+
}
57+
58+
public void Dispose()
59+
{
60+
ClearMemory();
61+
}
62+
}
63+
64+
}

0 commit comments

Comments
 (0)