Skip to content

Commit 9ef432b

Browse files
committed
sync
1 parent fe89db5 commit 9ef432b

4 files changed

Lines changed: 70 additions & 28 deletions

File tree

monticore-test/01.experiments/interpreter/src/main/grammars/SimpleEquations.mc4

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
grammar SimpleEquations extends Numerals, de.monticore.symbols.BasicSymbols {
44

5-
symbol scope SimpleEquationCompilationUnit = Name "{" (ProgramBlock | FunctionDefinition )+ "}";
6-
scope ProgramBlock = (Statement ";" | FunctionCall ";")* (Expression ";")? ;
5+
symbol scope SimpleEquationCompilationUnit = Name "{" (ProgramBlock | FunctionDefinition)+ "}";
6+
scope ProgramBlock = (Statement ";" | FunctionCall ";")+ (Expression ";")? ;
77

88
interface Statement;
99
interface Expression;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package simpleequations._symboltable;
2+
3+
public class SimpleEquationsScopesGenitorDelegator extends SimpleEquationsScopesGenitorDelegatorTOP{
4+
5+
@Override
6+
public simpleequations._symboltable.ISimpleEquationsArtifactScope createFromAST (simpleequations._ast.ASTSimpleEquationCompilationUnit rootNode) {
7+
simpleequations._symboltable.ISimpleEquationsArtifactScope as = symbolTable.createFromAST(rootNode);
8+
//only add this here
9+
as.setName(rootNode.getName());
10+
//until here
11+
if (as.isPresentName()){
12+
if (!as.getPackageName().isEmpty()){
13+
globalScope.addLoadedFile(as.getPackageName() + "." + as.getName());
14+
} else {
15+
globalScope.addLoadedFile(as.getName());
16+
}
17+
}
18+
return as;
19+
}
20+
}

monticore-test/01.experiments/interpreter/src/main/java/simpleequations/_visitor/SimpleEquationsInterpreter.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,20 @@ public SimpleEquationsInterpreter() {
2727
super();
2828
}
2929

30-
public MIValue interpret(ASTProgramBlock node) {
31-
MIValue result = new ErrorMIValue("Error ASTProgram node");
3230

31+
public MIValue interpret(ASTSimpleEquationCompilationUnit node) {
32+
MIValue result = new ErrorMIValue("Error ASTSimpleEquationCompilationUnit node");
3333
for (ASTFunctionDefinition funcDef : node.getFunctionDefinitionList()) {
3434
funcDef.evaluate(getRealThis());
3535
}
36+
for(ASTProgramBlock progBlock : node.getProgramBlockList()){
37+
progBlock.evaluate(getRealThis());
38+
}
39+
return result;
40+
}
41+
42+
public MIValue interpret(ASTProgramBlock node) {
43+
MIValue result = new ErrorMIValue("Error ASTProgram node");
3644

3745
for (ASTStatement s : node.getStatementList()) {
3846
result = s.evaluate(getRealThis());

monticore-test/01.experiments/interpreter/src/test/java/simpleequations/_visitor/SimpleEquationsInterpreterTest.java

Lines changed: 38 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import org.junit.jupiter.api.Test;
66
import simpleequations.SimpleEquationsMill;
77
import simpleequations._ast.ASTProgramBlock;
8+
import simpleequations._ast.ASTSimpleEquationCompilationUnit;
89
import simpleequations._parser.SimpleEquationsParser;
910
import simpleequations._symboltable.SimpleEquationsScopesGenitorDelegator;
1011

@@ -22,30 +23,33 @@ public void test() throws IOException {
2223
SimpleEquationsInterpreter interpreter = new SimpleEquationsInterpreter();
2324
SimpleEquationsScopesGenitorDelegator delegator = SimpleEquationsMill.scopesGenitorDelegator();
2425

25-
ASTProgramBlock program = parser.parse_StringProgramBlock("" +
26-
"int a = 3; " +
27-
"int b = 3; " +
28-
"int func func1(int a, int b){ " +
29-
" int c = a; " +
30-
" if( b > 0 ){ " +
31-
" b = b - 1; " +
32-
" a = a;" +
33-
" c = a * 3; " +
34-
" int d = func1(c, b);" +
35-
" return d;" +
36-
" } else { " +
37-
" return a;" +
38-
" };" +
39-
"}"+
40-
"int result = func1(a, b);" +
41-
"print(result);").get();
26+
ASTSimpleEquationCompilationUnit program = parser.parse_StringSimpleEquationCompilationUnit("" +
27+
"Program {" +
28+
" int a = 3; " +
29+
" int b = 3; " +
30+
" int func func1(int a, int b){ " +
31+
" int c = a; " +
32+
" if( b > 0 ){ " +
33+
" b = b - 1; " +
34+
" a = a;" +
35+
" c = a * 3; " +
36+
" int d = func1(c, b);" +
37+
" return d;" +
38+
" } else { " +
39+
" return a;" +
40+
" };" +
41+
" }"+
42+
" int result = func1(a, b);" +
43+
" print(result);" +
44+
"}").get();
4245
delegator.createFromAST(program);
4346
MIValue functionResult = interpreter.interpret(program);
4447
assertTrue(functionResult.isInt());
4548
assertEquals(81, functionResult.asInt());
4649

4750
//test recursive method definition
48-
//program = parser.parse_StringProgramBlock("" +
51+
//program = parser.parse_StringSimpleEquationCompilationUnit("" +
52+
// "Program {" +
4953
// "int a = 3;" +
5054
// "int b = 3;" +
5155
// "int func func1(int a, int b){" +
@@ -57,14 +61,22 @@ public void test() throws IOException {
5761
// " return func1(a,b);" +
5862
// "};" +
5963
// "int result = func1(a,b); " +
60-
// "print(result);").get();
64+
// "print(result);"
65+
// "}").get();
6166
//delegator.createFromAST(program);
6267
//MIValue recursiveResult = interpreter.interpret(program);
6368
//assertTrue(recursiveResult.isInt());
6469
//assertEquals(18, recursiveResult.asInt() );
6570

6671

67-
program = parser.parse_StringProgramBlock("var a=3.5; var b=4; print(a); var c=a+b; c;").get();
72+
program = parser.parse_StringSimpleEquationCompilationUnit("" +
73+
"Program {" +
74+
" var a=3.5; " +
75+
" var b=4; " +
76+
" print(a); " +
77+
" var c=a+b; " +
78+
" c;" +
79+
"}").get();
6880

6981
delegator.createFromAST(program);
7082
MIValue result = interpreter.interpret(program);
@@ -75,10 +87,12 @@ public void test() throws IOException {
7587
SimpleEquationsMill.reset();
7688
SimpleEquationsMill.init();
7789
interpreter = new SimpleEquationsInterpreter();
78-
program = parser.parse_StringProgramBlock(
79-
"var a = 40; " +
80-
"a = 45;" +
81-
"a;").get();
90+
program = parser.parse_StringSimpleEquationCompilationUnit(
91+
"Program {" +
92+
" var a = 40; " +
93+
" a = 45;" +
94+
" a;" +
95+
"}").get();
8296

8397
delegator.createFromAST(program);
8498
result = interpreter.interpret(program);

0 commit comments

Comments
 (0)