Skip to content

Commit a48e30d

Browse files
committed
piccode: Fixed bugs on the repl and clean up the API
1 parent 6c05bd7 commit a48e30d

File tree

2 files changed

+44
-16
lines changed

2 files changed

+44
-16
lines changed

src/main/java/org/piccode/backend/Compiler.java

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
import org.piccode.rt.modules.PiccodeSystemModule;
2727
import org.piccode.rt.modules.PiccodeTimeModule;
2828
import org.piccode.rt.modules.PiccodeTupleModule;
29+
import org.piccode.rt.modules.PiccodeTypesModule;
30+
import org.piccode.rt.modules.PiccodeVirtualModule;
2931

3032
/**
3133
*
@@ -40,13 +42,7 @@ public static PiccodeValue compile(String file, String code) {
4042
public static PiccodeValue compile(String file, String code, List<PiccodeValue> args) {
4143
try {
4244
var result = program(file, code);
43-
44-
var scope_id = new IdentifierAst("globalScope");
45-
scope_id.column = 0;
46-
scope_id.line = 1;
47-
scope_id.file = file;
48-
49-
prepareGlobalScope(scope_id);
45+
prepareGlobalScope(file);
5046
addGlobalFunctions();
5147

5248
PiccodeValue res = new PiccodeUnit();
@@ -59,17 +55,20 @@ public static PiccodeValue compile(String file, String code, List<PiccodeValue>
5955
}
6056

6157
if (has_main) {
62-
return new CallAst(new IdentifierAst("main"), List.of()).execute();
58+
var _result = new CallAst(new IdentifierAst("main"), List.of()).execute();
59+
Context.top.dropStackFrame();
60+
return _result;
6361
}
6462

6563
Context.top.dropStackFrame();
6664
return res;
6765
} catch (PiccodeException e) {
66+
Context.top.dropStackFrame();
6867
e.reportError();
6968
//e.printStackTrace();
7069
return new PiccodeUnit();
7170
} catch (Exception rte) {
72-
//Context.top.dropStackFrame();
71+
Context.top.dropStackFrame();
7372
rte.printStackTrace();
7473
return new PiccodeUnit();
7574
}
@@ -102,13 +101,22 @@ private static String sanitizeSourceFile(String code) {
102101
return String.join("\n", lines);
103102
}
104103

105-
public static void prepareGlobalScope(Ast parent) {
106-
Context.top.pushStackFrame(parent);
107-
Context.top.addGlobal("true", new PiccodeBoolean("true"));
104+
public static void prepareGlobalScope(String file) {
105+
prepareGlobalScope(file, "globalScope");
106+
}
107+
108+
public static void prepareGlobalScope(String file, String globalScopeName) {
109+
var scope_id = new IdentifierAst(globalScopeName);
110+
scope_id.column = 0;
111+
scope_id.line = 1;
112+
scope_id.file = file;
113+
Context.top.pushStackFrame(scope_id);
114+
Context.top.putLocal("true", new PiccodeBoolean("true"));
108115
Context.top.putLocal("false", new PiccodeBoolean("false"));
109116
addGlobalFunctions();
110117
}
111118

119+
112120
private static void addGlobalFunctions() {
113121
PiccodeIOModule.addFunctions();
114122
PiccodeArrayModule.addFunctions();
@@ -117,6 +125,8 @@ private static void addGlobalFunctions() {
117125
PiccodeMathModule.addFunctions();
118126
PiccodeSystemModule.addFunctions();
119127
PiccodeTimeModule.addFunctions();
128+
PiccodeTypesModule.addFunctions();
129+
PiccodeVirtualModule.addFunctions();
120130
}
121131

122132
}

src/main/java/org/piccode/piccodescript/Piccode.java

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
import org.jline.utils.AttributedStyle;
3636
import org.piccode.ast.IdentifierAst;
3737
import org.piccode.platf.Platforms;
38+
import org.piccode.rt.Context;
39+
import org.piccode.rt.PiccodeException;
3840
import org.piccode.rt.ReplState;
3941

4042
/**
@@ -43,7 +45,7 @@
4345
*/
4446
public class Piccode {
4547

46-
private static double VERSION = 0.2;
48+
private static double VERSION = 0.3;
4749

4850
public static void main(String[] args) {
4951
if (Platforms.isWindows()) {
@@ -300,6 +302,8 @@ public AttributedString highlight(LineReader reader, String buffer) {
300302

301303
var is_inner = false;
302304
ReplState.ACTIVE = true;
305+
306+
Compiler.prepareGlobalScope("repl", "REPL");
303307
outer:
304308
while (true) {
305309
StringBuilder inputBlock = new StringBuilder();
@@ -331,11 +335,25 @@ public AttributedString highlight(LineReader reader, String buffer) {
331335
continue;
332336
}
333337

334-
var result = Compiler.compile("repl", code, user_args);
335-
terminal.writer().println(result + " : " + result.type());
336-
terminal.flush();
338+
var result = Compiler.program("repl", code);
339+
PiccodeValue res = null;
340+
try {
341+
for (var stmt : result.nodes) {
342+
res = stmt.execute();
343+
}
344+
345+
terminal.writer().println(res + " : " + res.type());
346+
terminal.flush();
347+
} catch (PiccodeException e) {
348+
e.reportError();
349+
var stack_size = Context.top.getFramesCount();
350+
if (stack_size > 1) {
351+
Context.top.dropStackFrame();
352+
}
353+
}
337354
}
338355

356+
Context.top.dropStackFrame();
339357
ReplState.ACTIVE = false;
340358
terminal.writer().println("Exiting REPL");
341359
} catch (IOException | EndOfFileException | UserInterruptException e) {

0 commit comments

Comments
 (0)