@@ -37,45 +37,47 @@ public String toString() {
3737
3838 @ Override
3939 public PiccodeValue execute (Integer frame ) {
40- if (lhs instanceof CCOperationAst op ) {
41- var mod = (PiccodeModule ) op .execute (frame );
42- if (!(rhs instanceof CallAst ) && !(rhs instanceof IdentifierAst )) {
43- throw new PiccodeException (file , line , column , "No node " + rhs + " found in module " + mod .name );
40+ return Ast .safeExecute (frame , this , (expr ) -> {
41+ if (lhs instanceof CCOperationAst op ) {
42+ var mod = (PiccodeModule ) op .execute (frame );
43+ if (!(rhs instanceof CallAst ) && !(rhs instanceof IdentifierAst )) {
44+ throw new PiccodeException (file , line , column , "No node " + rhs + " found in module " + Chalk .on (mod .name ).green ());
45+ }
46+
47+ var id = new IdentifierAst (mod .name );
48+ id .file = file ;
49+ id .line = line ;
50+ id .column = column ;
51+ return process (id , mod , frame );
4452 }
45-
46- var id = new IdentifierAst (mod .name );
47- id .file = file ;
48- id .line = line ;
49- id .column = column ;
50- return process (id , mod , frame );
51- }
52-
53- if (lhs instanceof IdentifierAst id && Context .top .getValue (id .text ) != null ) {
54- var mod = Context .top .getValue (id .text );
55- if (!(rhs instanceof CallAst ) && !(rhs instanceof IdentifierAst )) {
56- throw new PiccodeException (file , line , column , "No node " + rhs + " found in module " + id .text );
53+
54+ if (lhs instanceof IdentifierAst id && Context .top .getValue (id .text ) != null ) {
55+ var mod = Context .top .getValue (id .text );
56+ if (!(rhs instanceof CallAst ) && !(rhs instanceof IdentifierAst )) {
57+ throw new PiccodeException (file , line , column , "No node " + rhs + " found in module " + Chalk .on (id .text ).green ());
58+ }
59+ return process (id , (PiccodeModule ) mod , frame );
5760 }
58- return process (id , (PiccodeModule )mod , frame );
59- }
6061
61- var err = new PiccodeException (file , line , column , "Invalid use of `::`. Expected a module on the lhs, but found " + Chalk .on (lhs .toString ()).red ());
62- err .frame = frame ;
62+ var err = new PiccodeException (file , line , column , "Invalid use of `::`. Expected a module on the lhs, but found " + Chalk .on (lhs .toString ()).red ());
63+ err .frame = frame ;
6364
64- if (lhs instanceof IdentifierAst id ) {
65- var nm = Context .top .getSimilarName (id .text );
66- if (nm != null && !nm .isEmpty ()) {
67- var note = new PiccodeSimpleNote ("Did you mean `" + Chalk .on (nm ).green () + "` instead of `" + Chalk .on (id .text ).red () + "` ?" );
68- err .addNote (note );
65+ if (lhs instanceof IdentifierAst id ) {
66+ var nm = Context .top .getSimilarName (id .text );
67+ if (nm != null && !nm .isEmpty ()) {
68+ var note = new PiccodeSimpleNote ("Did you mean `" + Chalk .on (nm ).green () + "` instead of `" + Chalk .on (id .text ).red () + "` ?" );
69+ err .addNote (note );
70+ }
6971 }
70- }
71- throw err ;
72+ throw err ;
73+ }) ;
7274 }
7375
7476 private PiccodeValue process (IdentifierAst id , PiccodeModule mod , Integer frame ) {
7577 var ctx = frame == null
76- ? Context .top
77- : Context .getContextAt (frame );
78-
78+ ? Context .top
79+ : Context .getContextAt (frame );
80+
7981 if (rhs instanceof IdentifierAst _id ) {
8082 for (var node : mod .nodes ) {
8183 if (node instanceof VarDecl vd && vd .name .equals (_id .text )) {
@@ -85,11 +87,11 @@ private PiccodeValue process(IdentifierAst id, PiccodeModule mod, Integer frame)
8587 node .execute (frame );
8688 var result = ctx .getValue (_id .text );
8789 if (result == null ) {
88- var err = new PiccodeException (func .file , func .line , func .column , "Function `" + _id .text + "` is not defined" );
90+ var err = new PiccodeException (func .file , func .line , func .column , "Function `" + Chalk . on ( _id .text ). red () + "` is not defined" );
8991 err .frame = frame ;
9092 var nm = ctx .getSimilarName (_id .text );
9193 if (nm != null && !nm .isEmpty ()) {
92- var note = new PiccodeException (func .file , func .line , func .column , "Maybe you meant `" + nm + "`" );
94+ var note = new PiccodeException (func .file , func .line , func .column , "Maybe you meant `" + Chalk . on ( nm ). green () + "`" );
9395 err .addNote (note );
9496 }
9597 throw err ;
@@ -101,15 +103,15 @@ private PiccodeValue process(IdentifierAst id, PiccodeModule mod, Integer frame)
101103 }
102104 }
103105
104- var err = new PiccodeException (file , line , column , "No function or identifier " + _id .text + " found in module " + id .text );
106+ var err = new PiccodeException (file , line , column , "No function or identifier " + Chalk . on ( _id .text ). red () + " found in module " + Chalk . on ( id .text ). green () );
105107 err .frame = frame ;
106108 throw err ;
107109 }
108110
109111 var call = (CallAst ) rhs ;
110112
111113 if (!(call .expr instanceof IdentifierAst )) {
112- var err = new PiccodeException (file , line , column , "Invalid function reference in module access module " + id .text + ": " + call .expr );
114+ var err = new PiccodeException (file , line , column , "Invalid function reference in module access module " + Chalk . on ( id .text ). red () + ": " + call .expr );
113115 err .frame = frame ;
114116 throw err ;
115117 }
@@ -120,7 +122,7 @@ private PiccodeValue process(IdentifierAst id, PiccodeModule mod, Integer frame)
120122 return node .execute (frame );
121123 }
122124 if (node instanceof FunctionAst func && func .name .equals (_id .text )) {
123- return safeExecute (frame , func , (expr ) -> {
125+ return Ast . safeExecute (frame , func , (expr ) -> {
124126 node .execute (frame );
125127 return call .execute (frame );
126128 });
@@ -131,7 +133,7 @@ private PiccodeValue process(IdentifierAst id, PiccodeModule mod, Integer frame)
131133 }
132134 }
133135
134- var err = new PiccodeException (file , line , column , "No function or identifier " + _id .text + " found in module " + id .text );
136+ var err = new PiccodeException (file , line , column , "No function or identifier " + Chalk . on ( _id .text ). red () + " found in module " + Chalk . on ( id .text ). green () );
135137 err .frame = frame ;
136138 throw err ;
137139 }
@@ -141,17 +143,4 @@ public String codeGen(TargetEnvironment target) {
141143 return String .format ("%s.%s" , lhs , rhs );
142144 }
143145
144- private PiccodeValue safeExecute (Integer frame , Ast expr , Function <Ast , PiccodeValue > func ) {
145- var ctx = frame == null
146- ? Context .top
147- : Context .getContextAt (frame );
148-
149- try {
150- ctx .pushStackFrame (expr );
151- return func .apply (expr );
152- } catch (PiccodeReturnException | PiccodeException exception ) {
153- ctx .dropStackFrame ();
154- throw exception ;
155- }
156- }
157146}
0 commit comments