@@ -25,7 +25,6 @@ public class LangInterpreter extends LexLangBaseVisitor<Value> {
2525 FunctionManager functionManager ;
2626
2727 Boolean returnCalled = false ;
28- List <Value > returnValues = null ;
2928
3029 public LangInterpreter (SemanticAnalyzer analyzer ) {
3130 this .functionManager = analyzer .getFuncManager ();
@@ -156,56 +155,45 @@ public Value visitInstancePexp(LexLangParser.InstancePexpContext ctx) {
156155 }
157156 if (List .of ("Int" , "Char" , "Bool" , "Float" ).contains (type ))
158157 return new Value (null );
159- // if (!dataTypes.containsKey(type))
160- // throw new LangException("Data '" + type + "' not found");
161158 return new Value (new Data (dataTypes .get (type )));
162159 }
163160
164161 // functions
165- // @Override
166- // public Value visitFunc(LexLangParser.FuncContext ctx) {
167- // functionManager.addFunction(ctx);
168- // return Value.VOID;
169- // }
170162
171163 @ Override
172164 public Value visitFuncCmd (LexLangParser .FuncCmdContext ctx ) {
173165 String name = ctx .ID ().getText ();
174- Value ret = runFunction (name , ctx .exps ());
175- for (int i = 0 ; i < ctx .lvalue ().size (); i ++) {
176- resolveVariable (ctx .lvalue (i ), returnValues .get (i ));
166+ Value returnValue = runFunction (name , ctx .exps ());
167+ if (ctx .lvalue ().size () > 0 ) {
168+ ArrayList <Value > returnList = (ArrayList <Value >) returnValue .getPrimitive ();
169+ for (int i = 0 ; i < ctx .lvalue ().size (); i ++)
170+ resolveVariable (ctx .lvalue (i ), returnList .get (i ));
177171 }
178- returnValues = null ;
179- return ret ;
172+ return returnValue ;
180173 }
181174
182175 @ Override
183176 public Value visitFuncCallPexp (LexLangParser .FuncCallPexpContext ctx ) {
184177 String name = ctx .ID ().getText ();
185- Value result = runFunction (name , ctx .exps ());
178+ Value returnValue = runFunction (name , ctx .exps ());
179+ ArrayList <Value > returnList = (ArrayList <Value >) returnValue .getPrimitive ();
186180 if (ctx .exp () != null ) {
187181 int i = visit (ctx .exp ()).getInt ();
188- // if (returnValues == null)
189- // throw new LangException("Function '" + name +
190- // "' doesn't returns arguments, tried to access argument [" + i + ']');
191- // if (i >= returnValues.size())
192- // throw new LangException("Function '" + name +
193- // "' only returns " + returnValues.size() +
194- // " arguments, tried to access argument [" + i + ']');
195- result = returnValues .get (i );
182+ returnValue = returnList .get (i );
196183 }
197- returnValues = null ;
198- return result ;
184+ else returnValue = returnList . get ( 0 ) ;
185+ return returnValue ;
199186 }
200187
201188 @ Override
202189 public Value visitReturnCmd (LexLangParser .ReturnCmdContext ctx ) {
203- this . returnValues = new ArrayList <>();
190+ ArrayList < Value > ret = new ArrayList <Value >();
204191 for (LexLangParser .ExpContext expContext : ctx .exp ()) {
205- this .returnValues .add (visit (expContext ));
192+ ret .add (visit (expContext ));
193+
206194 }
207195 this .returnCalled = true ;
208- return this . returnValues . get ( 0 );
196+ return new Value ( ret );
209197 }
210198
211199 // variables
@@ -386,7 +374,7 @@ public Value visitReadCmd(LexLangParser.ReadCmdContext ctx) {
386374 String response = reader .nextLine ();
387375 int val ;
388376 try {
389- val = Integer .parseInt (response );
377+ val = Integer .valueOf (response );
390378 } catch (Exception e ) {
391379 throw new LangException ("Read error: 'Int' expected, received '" + response + "'" , ctx );
392380 }
0 commit comments