diff options
Diffstat (limited to 'src/ast/nodes/AtomNode.java')
-rw-r--r-- | src/ast/nodes/AtomNode.java | 43 |
1 files changed, 38 insertions, 5 deletions
diff --git a/src/ast/nodes/AtomNode.java b/src/ast/nodes/AtomNode.java index 4dcb926..ceafc07 100644 --- a/src/ast/nodes/AtomNode.java +++ b/src/ast/nodes/AtomNode.java @@ -1,30 +1,63 @@ package ast.nodes; +import ast.types.*; import java.util.ArrayList; - +import java.util.regex.Matcher; +import java.util.regex.Pattern; import semanticanalysis.SemanticError; import semanticanalysis.SymbolTable; -import ast.types.*; /** * Node for the `atom` statement of the grammar. */ public class AtomNode implements Node { + protected String val; public AtomNode(String val) { this.val = val; } + public String getId() { + return this.val; + } + @Override public ArrayList<SemanticError> checkSemantics(SymbolTable ST, int _nesting) { - return new ArrayList<SemanticError>(); + var errors = new ArrayList<SemanticError>(); + + if ((this.typeCheck() instanceof AtomType) && ST.nslookup(this.getId()) < 0) { + errors.add(new SemanticError("'" + this.getId() + "' is not defined.")); + } + + return errors; } - // FIXME: this type for atom + // ENHANCE: return more specific types @Override public Type typeCheck() { - return new AtomType(); + 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 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 { + return new VoidType(); // could be any type of data + } } // TODO: add code generation for atom node |