From 3c4229fc9e0ec6da9a7f60b57b9e93c49d1b6b6c Mon Sep 17 00:00:00 2001 From: L0P0P Date: Thu, 27 Jun 2024 12:02:35 +0200 Subject: Fixed a lot of problems from all the progs we need to parse --- src/ast/nodes/AtomNode.java | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) (limited to 'src/ast/nodes/AtomNode.java') diff --git a/src/ast/nodes/AtomNode.java b/src/ast/nodes/AtomNode.java index 96132d8..ceafc07 100644 --- a/src/ast/nodes/AtomNode.java +++ b/src/ast/nodes/AtomNode.java @@ -26,11 +26,7 @@ public class AtomNode implements Node { public ArrayList checkSemantics(SymbolTable ST, int _nesting) { var errors = new ArrayList(); - // Print the symbol table - // System.out.println(ST); - if ((this.typeCheck() instanceof AtomType) && ST.nslookup(this.getId()) < 0) { - // System.out.println(!(this.typeCheck() instanceof IntType) + " " + !ST.top_lookup(this.getId())); errors.add(new SemanticError("'" + this.getId() + "' is not defined.")); } @@ -40,15 +36,26 @@ public class AtomNode implements Node { // ENHANCE: return more specific types @Override public Type typeCheck() { + Pattern booleanVariable = Pattern.compile("^(True|False)$"); + Pattern continueBreakVariable = Pattern.compile("^(continue|break)$"); // this regex should match every possible atom name written in this format: CHAR (CHAR | DIGIT)* - Pattern pattern = Pattern.compile("^[a-zA-Z][a-zA-Z0-9]*$", Pattern.CASE_INSENSITIVE); - Matcher matcher = pattern.matcher(this.val); - boolean matchFound = matcher.find(); - if (matchFound) { - // System.out.println("Match found for " + this.val); + Pattern simpleVariable = Pattern.compile("^[a-zA-Z][a-zA-Z0-9]*$", Pattern.CASE_INSENSITIVE); + + Matcher booleanVariableMatcher = booleanVariable.matcher(this.val); + Matcher continueBreakVariableMatcher = continueBreakVariable.matcher(this.val); + Matcher simpleVariableMatcher = simpleVariable.matcher(this.val); + + boolean matchFoundBoolean = booleanVariableMatcher.find(); + boolean matchFoundContinueBreak = continueBreakVariableMatcher.find(); + boolean matchFoundSimpleVariable = simpleVariableMatcher.find(); + + if (matchFoundBoolean) { + return new BoolType(); + } else if (matchFoundContinueBreak) { + return new ContinueBreakType(); + } else if (matchFoundSimpleVariable) { return new AtomType(); // could be a variable or a fuction } else { - // System.out.println("Match not found for " + this.val); return new VoidType(); // could be any type of data } } -- cgit v1.2.3-18-g5258