From d745f000d6d60b9cdf0ba40dbf80660c1aa6bc61 Mon Sep 17 00:00:00 2001 From: Jan Tuomi Date: Mon, 20 Jun 2016 20:55:22 +0300 Subject: Implemented general enhancements --- main/core/parser/Parser.java | 61 ++++++++++++++++++++++++++++++++++++-------- 1 file changed, 50 insertions(+), 11 deletions(-) (limited to 'main/core/parser/Parser.java') diff --git a/main/core/parser/Parser.java b/main/core/parser/Parser.java index e8b8813..f6e1208 100644 --- a/main/core/parser/Parser.java +++ b/main/core/parser/Parser.java @@ -3,9 +3,10 @@ package com.jantuomi.interpreter.main.core.parser; import com.jantuomi.interpreter.main.core.parser.ast.ASTNode; import com.jantuomi.interpreter.main.core.tokenizer.token.ArgumentInfo; import com.jantuomi.interpreter.main.core.tokenizer.token.Token; +import com.jantuomi.interpreter.main.exception.ExceptionManager; +import com.jantuomi.interpreter.main.exception.InterpreterException; import java.util.ArrayList; -import java.util.Collections; import java.util.List; import java.util.Stack; @@ -28,11 +29,11 @@ public class Parser { private Stack stack; - public List parse(List tokens) { + public List parse(List tokens) throws InterpreterException { this.tokens = tokens; this.stack = new Stack<>(); - List args = new ArrayList<>(); + List args; List output = new ArrayList<>(); for (Token t : tokens) { if (t.getTokenType() == Token.Type.NewlineToken) { @@ -42,16 +43,48 @@ public class Parser { continue; } - args.clear(); + args = new ArrayList<>(); ArgumentInfo argumentInfo = t.getArgumentInfo(); - if (!argumentInfo.getVarargs()) { - for (int i = 0; i < argumentInfo.getCount(); i++) { + + /* + This branch is executed if the token has a variable list of arguments + */ + if (argumentInfo.getVarargs()) { + while (true) { + if (stack.size() > 0) { + Token arg = stack.pop(); + if (arg.getTokenType() != argumentInfo.getTerminator()) { + args.add(arg); + } + else { + args.add(arg); + break; + } + } + } + } + /* + If the token has an optional argument type (it is only an argument if + it is of certain type) + */ + else if (argumentInfo.getOptionalArgument() != null) { + if (stack.size() > 0 && stack.peek().getTokenType() == argumentInfo.getOptionalArgument()) { args.add(stack.pop()); } - } else { - System.err.println("no vararg support yet"); } - Collections.reverse(args); + /* Normal cases with a fixed list of arguments */ + else { + for (int i = 0; i < argumentInfo.getCount(); i++) { + if (stack.size() > 0) { + args.add(stack.pop()); + } else { + ExceptionManager.raise(InterpreterException.ExceptionType.ArgumentError, t.getLine(), + t.toString(), Integer.toString(argumentInfo.getCount())); + return null; + } + } + } + Token result = t.setArguments(args); stack.push(result); } @@ -62,8 +95,14 @@ public class Parser { } public void printTree(Token e) { - System.out.println("### Tree begin ###"); + System.out.println("### Token Tree begin ###"); e.print(0); - System.out.println("### Tree end ###"); + System.out.println("### Token Tree end ###"); + } + + public void printAllTrees(List trees) { + for (Token tree : trees) { + printTree(tree); + } } } -- cgit v1.3