From 6bdf1fc6c1b7afe18ffcae05f8fb11eca0f51258 Mon Sep 17 00:00:00 2001 From: Santo Cariotti Date: Tue, 25 Jun 2024 11:33:41 +0200 Subject: wip --- src/ast/nodes/AtomNode.java | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) (limited to 'src/ast/nodes/AtomNode.java') diff --git a/src/ast/nodes/AtomNode.java b/src/ast/nodes/AtomNode.java index 4dcb926..7dc38fb 100644 --- a/src/ast/nodes/AtomNode.java +++ b/src/ast/nodes/AtomNode.java @@ -16,15 +16,33 @@ public class AtomNode implements Node { this.val = val; } + public String getId() { + return this.val; + } + @Override public ArrayList checkSemantics(SymbolTable ST, int _nesting) { - return new ArrayList(); + var errors = new ArrayList(); + System.out.println(getId() + " " + _nesting + " " + ST.nslookup(getId())); + if (!(this.typeCheck() instanceof IntType) && !ST.top_lookup(this.getId())) { + System.out.println(!(this.typeCheck() instanceof IntType) + " " + !ST.top_lookup(this.getId())); + errors.add(new SemanticError("Undefined name `" + this.getId() + "`")); + } + + return errors; } // FIXME: this type for atom @Override public Type typeCheck() { - return new AtomType(); + try { + Integer.parseInt(this.val); + System.out.println(this.val + " is int"); + return new IntType(); + } catch (NumberFormatException e) { + System.out.println(this.val + " is atom"); + return new AtomType(); + } } // TODO: add code generation for atom node -- cgit v1.2.3-18-g5258 From b12c01732860f9727626829e0b25a273de5fe5c7 Mon Sep 17 00:00:00 2001 From: geno Date: Tue, 25 Jun 2024 16:04:07 +0200 Subject: check semantic of defined and undefined variables implemented do not check for built-in function works on the example --- src/ast/nodes/AtomNode.java | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) (limited to 'src/ast/nodes/AtomNode.java') diff --git a/src/ast/nodes/AtomNode.java b/src/ast/nodes/AtomNode.java index 7dc38fb..4c9a807 100644 --- a/src/ast/nodes/AtomNode.java +++ b/src/ast/nodes/AtomNode.java @@ -1,15 +1,17 @@ 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) { @@ -23,25 +25,28 @@ public class AtomNode implements Node { @Override public ArrayList checkSemantics(SymbolTable ST, int _nesting) { var errors = new ArrayList(); - System.out.println(getId() + " " + _nesting + " " + ST.nslookup(getId())); - if (!(this.typeCheck() instanceof IntType) && !ST.top_lookup(this.getId())) { - System.out.println(!(this.typeCheck() instanceof IntType) + " " + !ST.top_lookup(this.getId())); + // System.out.println("[ATOM] id: " + getId() + " ns: " + _nesting + " top_lookup" + ST.top_lookup(this.getId())); + if ((this.typeCheck() instanceof AtomType) && !ST.top_lookup(this.getId())) { + // System.out.println(!(this.typeCheck() instanceof IntType) + " " + !ST.top_lookup(this.getId())); errors.add(new SemanticError("Undefined name `" + this.getId() + "`")); } return errors; } - // FIXME: this type for atom + // ENHANCE: return more specific types @Override public Type typeCheck() { - try { - Integer.parseInt(this.val); - System.out.println(this.val + " is int"); - return new IntType(); - } catch (NumberFormatException e) { - System.out.println(this.val + " is atom"); - return new AtomType(); + // 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); + 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 From fc712f94a7ed8554d8d44f4965be367354a7e670 Mon Sep 17 00:00:00 2001 From: L0P0P Date: Wed, 26 Jun 2024 10:06:12 +0200 Subject: Using child --- src/ast/nodes/AtomNode.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/ast/nodes/AtomNode.java') diff --git a/src/ast/nodes/AtomNode.java b/src/ast/nodes/AtomNode.java index 4c9a807..0a3c765 100644 --- a/src/ast/nodes/AtomNode.java +++ b/src/ast/nodes/AtomNode.java @@ -26,7 +26,7 @@ public class AtomNode implements Node { public ArrayList checkSemantics(SymbolTable ST, int _nesting) { var errors = new ArrayList(); // System.out.println("[ATOM] id: " + getId() + " ns: " + _nesting + " top_lookup" + ST.top_lookup(this.getId())); - if ((this.typeCheck() instanceof AtomType) && !ST.top_lookup(this.getId())) { + 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("Undefined name `" + this.getId() + "`")); } -- cgit v1.2.3-18-g5258 From 9e6c17cb44bc165e315ec039a0e09183716d2037 Mon Sep 17 00:00:00 2001 From: L0P0P Date: Wed, 26 Jun 2024 11:44:58 +0200 Subject: Semantic check for function declaration and function invocation --- src/ast/nodes/AtomNode.java | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'src/ast/nodes/AtomNode.java') diff --git a/src/ast/nodes/AtomNode.java b/src/ast/nodes/AtomNode.java index 0a3c765..0704bc4 100644 --- a/src/ast/nodes/AtomNode.java +++ b/src/ast/nodes/AtomNode.java @@ -25,10 +25,13 @@ public class AtomNode implements Node { @Override public ArrayList checkSemantics(SymbolTable ST, int _nesting) { var errors = new ArrayList(); - // System.out.println("[ATOM] id: " + getId() + " ns: " + _nesting + " top_lookup" + ST.top_lookup(this.getId())); - if ((this.typeCheck() instanceof AtomType) && ST.nslookup(this.getId()) < 0) { + + // Print the symbol table + System.out.println(ST); + + if ((this.typeCheck() instanceof AtomType) && !ST.top_lookup(this.getId())/*ST.nslookup(this.getId()) < 0*/) { // System.out.println(!(this.typeCheck() instanceof IntType) + " " + !ST.top_lookup(this.getId())); - errors.add(new SemanticError("Undefined name `" + this.getId() + "`")); + errors.add(new SemanticError("'" + this.getId() + "' is not defined.")); } return errors; -- cgit v1.2.3-18-g5258 From 06671f5aed68753435a762bc3be43e83094156d1 Mon Sep 17 00:00:00 2001 From: geno Date: Wed, 26 Jun 2024 14:53:05 +0200 Subject: exercise 1 completed 1.b now works 1.c implened error for "function takes N positional arguments but M were given" --- src/ast/nodes/AtomNode.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'src/ast/nodes/AtomNode.java') diff --git a/src/ast/nodes/AtomNode.java b/src/ast/nodes/AtomNode.java index 0704bc4..96132d8 100644 --- a/src/ast/nodes/AtomNode.java +++ b/src/ast/nodes/AtomNode.java @@ -25,11 +25,11 @@ public class AtomNode implements Node { @Override public ArrayList checkSemantics(SymbolTable ST, int _nesting) { var errors = new ArrayList(); - + // Print the symbol table - System.out.println(ST); + // System.out.println(ST); - if ((this.typeCheck() instanceof AtomType) && !ST.top_lookup(this.getId())/*ST.nslookup(this.getId()) < 0*/) { + 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.")); } -- cgit v1.2.3-18-g5258 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 From 673117132daa9c4fdd103189b5cd9a32a3731f5a Mon Sep 17 00:00:00 2001 From: geno Date: Thu, 27 Jun 2024 13:08:54 +0200 Subject: remove duplicates --- src/ast/nodes/AtomNode.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/ast/nodes/AtomNode.java') diff --git a/src/ast/nodes/AtomNode.java b/src/ast/nodes/AtomNode.java index ceafc07..58cd7f9 100644 --- a/src/ast/nodes/AtomNode.java +++ b/src/ast/nodes/AtomNode.java @@ -27,7 +27,7 @@ public class AtomNode implements Node { var errors = new ArrayList(); if ((this.typeCheck() instanceof AtomType) && ST.nslookup(this.getId()) < 0) { - errors.add(new SemanticError("'" + this.getId() + "' is not defined.")); + errors.add(new SemanticError("name '" + this.getId() + "' is not defined.")); } return errors; -- cgit v1.2.3-18-g5258 From 095a2cb4e9abb88805aac3271874bc512108ff96 Mon Sep 17 00:00:00 2001 From: geno Date: Thu, 27 Jun 2024 18:19:27 +0200 Subject: minor changes --- src/ast/nodes/AtomNode.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'src/ast/nodes/AtomNode.java') diff --git a/src/ast/nodes/AtomNode.java b/src/ast/nodes/AtomNode.java index 58cd7f9..3c2b1eb 100644 --- a/src/ast/nodes/AtomNode.java +++ b/src/ast/nodes/AtomNode.java @@ -28,6 +28,8 @@ public class AtomNode implements Node { if ((this.typeCheck() instanceof AtomType) && ST.nslookup(this.getId()) < 0) { errors.add(new SemanticError("name '" + this.getId() + "' is not defined.")); + } else { + // System.out.println("exist " + this.getId()); } return errors; @@ -40,11 +42,11 @@ public class AtomNode implements Node { 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(); -- cgit v1.2.3-18-g5258 From eb7fb04d2dfadd004fa86f28da42681e0038df06 Mon Sep 17 00:00:00 2001 From: geno Date: Thu, 27 Jun 2024 18:20:34 +0200 Subject: left, right = 1, 2 assignment covered sfaccimm ho fatto refactoring dell exprlist --- src/ast/nodes/AtomNode.java | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) (limited to 'src/ast/nodes/AtomNode.java') diff --git a/src/ast/nodes/AtomNode.java b/src/ast/nodes/AtomNode.java index 3c2b1eb..592aac4 100644 --- a/src/ast/nodes/AtomNode.java +++ b/src/ast/nodes/AtomNode.java @@ -13,9 +13,11 @@ import semanticanalysis.SymbolTable; public class AtomNode implements Node { protected String val; + protected TestlistCompNode exprlist; - public AtomNode(String val) { + public AtomNode(String val, Node exprlist) { this.val = val; + this.exprlist = (TestlistCompNode) exprlist; } public String getId() { @@ -26,10 +28,16 @@ public class AtomNode implements Node { public ArrayList checkSemantics(SymbolTable ST, int _nesting) { var errors = new ArrayList(); - if ((this.typeCheck() instanceof AtomType) && ST.nslookup(this.getId()) < 0) { - errors.add(new SemanticError("name '" + this.getId() + "' is not defined.")); - } else { - // System.out.println("exist " + this.getId()); + if (val != null) { + if ((this.typeCheck() instanceof AtomType) && ST.nslookup(this.getId()) < 0) { + errors.add(new SemanticError("name '" + this.getId() + "' is not defined.")); + } else { + // System.out.println("exist " + this.typeCheck()); + } + } + + if (exprlist != null) { + errors.addAll(exprlist.checkSemantics(ST, _nesting)); } return errors; @@ -38,6 +46,10 @@ public class AtomNode implements Node { // ENHANCE: return more specific types @Override public Type typeCheck() { + if (this.val == null) { + return new VoidType(); + } + 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)* -- cgit v1.2.3-18-g5258 From fd85e9980c0305c6dfb91aaeb199430a89163c3e Mon Sep 17 00:00:00 2001 From: geno Date: Thu, 27 Jun 2024 20:49:56 +0200 Subject: fixed comp_for and added reseved word type --- src/ast/nodes/AtomNode.java | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) (limited to 'src/ast/nodes/AtomNode.java') diff --git a/src/ast/nodes/AtomNode.java b/src/ast/nodes/AtomNode.java index 592aac4..16281d6 100644 --- a/src/ast/nodes/AtomNode.java +++ b/src/ast/nodes/AtomNode.java @@ -50,23 +50,28 @@ public class AtomNode implements Node { return new VoidType(); } + Pattern noneVariable = Pattern.compile("^(None)$"); Pattern booleanVariable = Pattern.compile("^(True|False)$"); - Pattern continueBreakVariable = Pattern.compile("^(continue|break)$"); + Pattern reservedWords = Pattern.compile("^(continue|break|int|float)$"); // 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 noneVariableMatcher = noneVariable.matcher(this.val); Matcher booleanVariableMatcher = booleanVariable.matcher(this.val); - Matcher continueBreakVariableMatcher = continueBreakVariable.matcher(this.val); + Matcher reservedWordsMatcher = reservedWords.matcher(this.val); Matcher simpleVariableMatcher = simpleVariable.matcher(this.val); + boolean matchFoundNone = noneVariableMatcher.find(); boolean matchFoundBoolean = booleanVariableMatcher.find(); - boolean matchFoundContinueBreak = continueBreakVariableMatcher.find(); + boolean matchFoundContinueBreak = reservedWordsMatcher.find(); boolean matchFoundSimpleVariable = simpleVariableMatcher.find(); if (matchFoundBoolean) { return new BoolType(); } else if (matchFoundContinueBreak) { - return new ContinueBreakType(); + return new ReservedWordsType(); + } else if (matchFoundNone) { + return new NoneType(); } else if (matchFoundSimpleVariable) { return new AtomType(); // could be a variable or a fuction } else { -- cgit v1.2.3-18-g5258 From d653d3598d71fea30d45d118e3d046a3aed53ac1 Mon Sep 17 00:00:00 2001 From: Santo Cariotti Date: Thu, 27 Jun 2024 22:39:22 +0200 Subject: Uncommented test files --- src/ast/nodes/AtomNode.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'src/ast/nodes/AtomNode.java') diff --git a/src/ast/nodes/AtomNode.java b/src/ast/nodes/AtomNode.java index 16281d6..cea617f 100644 --- a/src/ast/nodes/AtomNode.java +++ b/src/ast/nodes/AtomNode.java @@ -53,7 +53,8 @@ public class AtomNode implements Node { Pattern noneVariable = Pattern.compile("^(None)$"); Pattern booleanVariable = Pattern.compile("^(True|False)$"); Pattern reservedWords = Pattern.compile("^(continue|break|int|float)$"); - // this regex should match every possible atom name written in this format: CHAR (CHAR | DIGIT)* + // 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 noneVariableMatcher = noneVariable.matcher(this.val); @@ -87,6 +88,7 @@ public class AtomNode implements Node { @Override public String toPrint(String prefix) { + // FIXME: can be a testlist_comp with two expr and two atoms if (val != null) { return prefix + "Atom(" + val + ")\n"; } -- cgit v1.2.3-18-g5258 From 37665fb6d0bc1eb29396ae949354cf7d6f9d54ca Mon Sep 17 00:00:00 2001 From: geno Date: Fri, 28 Jun 2024 12:23:28 +0200 Subject: resolving all the comments santo made --- src/ast/nodes/AtomNode.java | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'src/ast/nodes/AtomNode.java') diff --git a/src/ast/nodes/AtomNode.java b/src/ast/nodes/AtomNode.java index cea617f..fef72ec 100644 --- a/src/ast/nodes/AtomNode.java +++ b/src/ast/nodes/AtomNode.java @@ -20,6 +20,10 @@ public class AtomNode implements Node { this.exprlist = (TestlistCompNode) exprlist; } + /** + * Returns the identifier of the `AtomNode` if it's not `null`, otherwise + * returns `null`. + */ public String getId() { return this.val; } -- cgit v1.2.3-18-g5258 From afff6a80cd58f7787efa1398f7c8cbdce8989323 Mon Sep 17 00:00:00 2001 From: geno Date: Sun, 30 Jun 2024 13:36:37 +0200 Subject: fixed warnings, better formatting, final added where possible --- src/ast/nodes/AtomNode.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/ast/nodes/AtomNode.java') diff --git a/src/ast/nodes/AtomNode.java b/src/ast/nodes/AtomNode.java index fef72ec..47a15d7 100644 --- a/src/ast/nodes/AtomNode.java +++ b/src/ast/nodes/AtomNode.java @@ -30,7 +30,7 @@ public class AtomNode implements Node { @Override public ArrayList checkSemantics(SymbolTable ST, int _nesting) { - var errors = new ArrayList(); + var errors = new ArrayList(); if (val != null) { if ((this.typeCheck() instanceof AtomType) && ST.nslookup(this.getId()) < 0) { -- cgit v1.2.3-18-g5258