|
13 | 13 | import org.piccode.ast.CallAst; |
14 | 14 | import org.piccode.ast.FunctionAst; |
15 | 15 | import org.piccode.ast.IdentifierAst; |
| 16 | +import org.piccode.ast.ImportAst; |
| 17 | +import org.piccode.ast.ModuleAst; |
16 | 18 | import org.piccode.ast.PiccodeVisitor; |
17 | 19 | import org.piccode.ast.ReturnAst; |
18 | 20 | import org.piccode.ast.StatementList; |
@@ -105,6 +107,44 @@ public static PiccodeValue compile(String file, String code, List<PiccodeValue> |
105 | 107 | } |
106 | 108 | } |
107 | 109 |
|
| 110 | + |
| 111 | + public static List<Ast> compileDeclarationsAndGetExpressions(String file, String code, List<PiccodeValue> args) { |
| 112 | + List<Ast> nodes = new ArrayList<>(); |
| 113 | + try { |
| 114 | + var result = program(file, code); |
| 115 | + prepareGlobalScope(file); |
| 116 | + |
| 117 | + for (var stmt : result.nodes) { |
| 118 | + if ((stmt instanceof ImportAst) || (stmt instanceof ModuleAst) || (stmt instanceof FunctionAst)) { |
| 119 | + stmt.execute(null); |
| 120 | + } else { |
| 121 | + nodes.add(stmt); |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + Context.top.dropStackFrame(); |
| 126 | + return nodes; |
| 127 | + } catch (PiccodeReturnException ret) { |
| 128 | + if (Context.top.getFramesCount() > 0) { |
| 129 | + Context.top.dropStackFrame(); |
| 130 | + } |
| 131 | + return nodes; |
| 132 | + } catch (PiccodeException e) { |
| 133 | + if (Context.top.getFramesCount() > 0) { |
| 134 | + Context.top.dropStackFrame(); |
| 135 | + } |
| 136 | + e.reportError(); |
| 137 | + return nodes; |
| 138 | + } catch (Exception rte) { |
| 139 | + if (Context.top.getFramesCount() > 0) { |
| 140 | + Context.top.dropStackFrame(); |
| 141 | + } |
| 142 | + rte.printStackTrace(); |
| 143 | + return nodes; |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + |
108 | 148 | public static List<Ast> parse(String file, String code) { |
109 | 149 | return program(file, code).nodes; |
110 | 150 | } |
|
0 commit comments