parser working

This commit is contained in:
nora 2021-01-17 10:41:43 +01:00
parent 0c334b5553
commit 6270363d96
4 changed files with 59 additions and 16 deletions

View file

@ -2,13 +2,14 @@ package com.github.nilstrieb.hunterlang.lexer;
import com.github.nilstrieb.hunterlang.hllibrary.FunctionArgLookup; import com.github.nilstrieb.hunterlang.hllibrary.FunctionArgLookup;
import com.github.nilstrieb.hunterlang.lib.ConsoleColors; import com.github.nilstrieb.hunterlang.lib.ConsoleColors;
import com.github.nilstrieb.hunterlang.parser.ParseTreeBodyNode;
import com.github.nilstrieb.hunterlang.parser.ParseTreeNode; import com.github.nilstrieb.hunterlang.parser.ParseTreeNode;
public class LexToken { public class LexToken {
WordType key; WordType key;
String value; String value;
private int argOverrideValue = -1; private int argOverrideValue = -1;
public LexToken(WordType key, String value) { public LexToken(WordType key, String value) {

View file

@ -170,20 +170,27 @@ public class Lexer {
} }
Gon wants true { Gon wants true {
Gon wants true { Gon wants true {
1 Leorio does say "that is actually very true"
}
}
""";
String ifif = """
Gon wants true {
Gon wants true {
Leorio does say "hallo"
} }
} }
"""; """;
String sif = """ String sif = """
Gon wants true { Gon wants true {
Leorio does say "false" Leorio does say "hallo"
}
"""; """;
String hierarchy = """ String hierarchy = """
killua0 hunts 3 > 3 killua0 hunts 3 > 3
"""; """;
ArrayList<LexToken> tokens = l.lex(sif); ArrayList<LexToken> tokens = l.lex(ifs);
Parser p = new Parser(); Parser p = new Parser();
try { try {

View file

@ -41,4 +41,8 @@ public class ParseTreeBodyNode extends ParseTreeNode{
Parser p = new Parser(); Parser p = new Parser();
statements = p.parse(tokens); statements = p.parse(tokens);
} }
public ArrayList<LexToken> getTokens() {
return tokens;
}
} }

View file

@ -3,9 +3,9 @@ package com.github.nilstrieb.hunterlang.parser;
import com.github.nilstrieb.hunterlang.lexer.LexToken; import com.github.nilstrieb.hunterlang.lexer.LexToken;
import com.github.nilstrieb.hunterlang.lexer.WordType; import com.github.nilstrieb.hunterlang.lexer.WordType;
import com.github.nilstrieb.hunterlang.lib.ConsoleColors; import com.github.nilstrieb.hunterlang.lib.ConsoleColors;
import com.sun.source.tree.LambdaExpressionTree;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator; import java.util.ListIterator;
public class Parser { public class Parser {
@ -19,10 +19,15 @@ public class Parser {
System.out.println(lexTokens); System.out.println(lexTokens);
} }
System.out.println(ConsoleColors.YELLOW_BACKGROUND_BRIGHT + ConsoleColors.BLACK_BOLD + "PARSER" + ConsoleColors.RESET);
ArrayList<ParseTreeNode> list = new ArrayList<>(); ArrayList<ParseTreeNode> list = new ArrayList<>();
ParseTreeNode parent = null; ParseTreeNode parent = null;
for (ArrayList<LexToken> statement : split) { for (ArrayList<LexToken> statement : split) {
if (statement.size() == 0) {
throw new ParseException("empty body");
}
ListIterator<LexToken> iterator = statement.listIterator(); ListIterator<LexToken> iterator = statement.listIterator();
LexToken current = iterator.next(); LexToken current = iterator.next();
@ -49,12 +54,39 @@ public class Parser {
while (current.getKey() != WordType.BOPEN) { while (current.getKey() != WordType.BOPEN) {
current = iterator.next(); current = iterator.next();
parent.addChild(current.toNode()); parent.addChild(current.toNode());
//BOPEN does not need to be a child
} }
//add body tokens
int brackets = 0; int brackets = 0;
while ((brackets != 0 || current.getKey() != WordType.BCLOSE) && iterator.hasNext()) {
current = iterator.next(); current = iterator.next();
while ((brackets != 0 || current.getKey() != WordType.BCLOSE) && iterator.hasNext()) {
parentBN.addToken(current); parentBN.addToken(current);
System.out.println("addt " + current);
current = iterator.next();
if (current.getKey() == WordType.BOPEN) {
brackets++;
} else if (current.getKey() == WordType.BCLOSE) {
if(brackets == 1){
parentBN.addToken(current);
}
brackets--;
}
}
parentBN.parse();
}
case ELSE -> {
parent = new ParseTreeBodyNode(current.toNode());
ParseTreeBodyNode parentBN = (ParseTreeBodyNode) parent;
int brackets = 0;
current = iterator.next();
if (current.getKey() != WordType.BOPEN) {
throw new ParseException("{ Expected: " + current.getKey());
}
current = iterator.next();
while ((brackets != 0 || current.getKey() != WordType.BCLOSE) && iterator.hasNext()) {
parentBN.addToken(current);
current = iterator.next();
if (current.getKey() == WordType.BOPEN) { if (current.getKey() == WordType.BOPEN) {
brackets++; brackets++;
} else if (current.getKey() == WordType.BCLOSE) { } else if (current.getKey() == WordType.BCLOSE) {
@ -62,14 +94,10 @@ public class Parser {
} }
} }
parentBN.parse(); parentBN.parse();
}
case ELSE -> {
} }
case LIBFUNCCALL -> { case LIBFUNCCALL -> {
parent = current.toNode();
} parent.addChild(evaluate(tokens.subList(1, tokens.size())));
case BCLOSE -> {
} }
default -> throw new ParseException("Unexpected value at start of statement: " + current.getKey()); default -> throw new ParseException("Unexpected value at start of statement: " + current.getKey());
@ -88,7 +116,7 @@ public class Parser {
* @param tokens The tokens * @param tokens The tokens
* @return The parent node * @return The parent node
*/ */
private ParseTreeNode evaluate(ArrayList<LexToken> tokens) throws ParseException { private ParseTreeNode evaluate(List<LexToken> tokens) throws ParseException {
ListIterator<LexToken> iterator = tokens.listIterator(); ListIterator<LexToken> iterator = tokens.listIterator();
LexToken prev; LexToken prev;
LexToken curr; LexToken curr;
@ -132,8 +160,11 @@ public class Parser {
ListIterator<LexToken> iterator = tokens.listIterator(); ListIterator<LexToken> iterator = tokens.listIterator();
if (tokens.size() < 2) { if (tokens.size() < 1) {
throw new ParseException("code has to consist of at least 2 tokens"); return tokensSplit;
} else if (tokens.size() == 1) {
tokensSplit.get(0).add(iterator.next());
return tokensSplit;
} }
LexToken prev; LexToken prev;