aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/cps/AST.h7
-rw-r--r--include/cps/Lexer.h2
-rw-r--r--lib/Lexer/Lexer.cc1
-rw-r--r--lib/Parser/Parser.cc16
-rw-r--r--lib/Parser/ParserExpression.cc4
-rw-r--r--lib/Parser/ParserFunction.cc16
-rw-r--r--tests/iii/class-destructor-duplicate-error/case.json8
-rw-r--r--tests/iii/class-destructor-duplicate-error/program.cps6
-rw-r--r--tests/iii/class-destructor-function-error/case.json8
-rw-r--r--tests/iii/class-destructor-function-error/program.cps5
-rw-r--r--tests/iii/class-destructor-global-error/case.json8
-rw-r--r--tests/iii/class-destructor-global-error/program.cps2
-rw-r--r--tests/iii/class-destructor-parameters-error/case.json8
-rw-r--r--tests/iii/class-destructor-parameters-error/program.cps4
14 files changed, 89 insertions, 6 deletions
diff --git a/include/cps/AST.h b/include/cps/AST.h
index c2754a5..f20b106 100644
--- a/include/cps/AST.h
+++ b/include/cps/AST.h
@@ -174,6 +174,13 @@ public:
ExprAST *getExpr() const { return Expr.get(); }
};
+class DeleteStmtAST : public StmtAST {
+ std::unique_ptr<ExprAST> Target;
+public:
+ explicit DeleteStmtAST(std::unique_ptr<ExprAST> T) : Target(std::move(T)) {}
+ ExprAST *getTarget() const { return Target.get(); }
+};
+
class InputStmtAST : public StmtAST {
std::unique_ptr<ExprAST> Target;
public:
diff --git a/include/cps/Lexer.h b/include/cps/Lexer.h
index 716dc4a..b465591 100644
--- a/include/cps/Lexer.h
+++ b/include/cps/Lexer.h
@@ -99,7 +99,7 @@ enum Token {
tok_c_char = -610,
tok_c_string = -611,
tok_c_double = -612,
- tok_new = -613,
+ tok_new = -613, tok_delete = -614,
tok_try = -700,
tok_catch = -701,
diff --git a/lib/Lexer/Lexer.cc b/lib/Lexer/Lexer.cc
index 8f86ced..61785c7 100644
--- a/lib/Lexer/Lexer.cc
+++ b/lib/Lexer/Lexer.cc
@@ -151,6 +151,7 @@ int Lexer::readToken() {
if (IdentifierStr == "SET") return tok_set;
if (IdentifierStr == "DEFINE") return tok_define;
if (IdentifierStr == "NEW") return tok_new;
+ if (IdentifierStr == "DELETE") return tok_delete;
if (IdentifierStr == "CLASS") return tok_class;
if (IdentifierStr == "ENDCLASS") return tok_endclass;
if (IdentifierStr == "PUBLIC") return tok_public;
diff --git a/lib/Parser/Parser.cc b/lib/Parser/Parser.cc
index d3b2daa..a7e7edf 100644
--- a/lib/Parser/Parser.cc
+++ b/lib/Parser/Parser.cc
@@ -45,7 +45,7 @@ std::vector<std::unique_ptr<StmtAST>> Parser::Parse() {
void Parser::syncToStatementStart() {
while (CurTok != tok_eof) {
switch (CurTok) {
- case tok_constant: case tok_declare: case tok_identifier: case tok_input: case tok_output:
+ case tok_delete: case tok_constant: case tok_declare: case tok_identifier: case tok_input: case tok_output:
case tok_case: case tok_endcase: case tok_otherwise:
case tok_if: case tok_while: case tok_repeat: case tok_for:
case tok_function: case tok_procedure: case tok_call: case tok_return:
@@ -94,6 +94,20 @@ std::unique_ptr<StmtAST> Parser::ParseStatement() {
}
std::unique_ptr<StmtAST> Parser::ParseStatementImpl() {
+ if (CurTok == tok_delete) {
+ int Line = Lex.getLine();
+ getNextToken();
+ if (CurTok != tok_identifier) {
+ fprintf(stderr, "error: DELETE requires an object designator at line %d\n", Line);
+ return nullptr;
+ }
+ std::string Name = Lex.IdentifierStr;
+ getNextToken();
+ std::vector<DesignatorAccess> Accesses;
+ if (!ParseDesignatorSuffix(Accesses)) return nullptr;
+ auto Target = std::make_unique<DesignatorExprAST>(Name, std::move(Accesses), Line);
+ return std::make_unique<DeleteStmtAST>(std::move(Target));
+ }
if (CurTok == tok_case) return ParseCaseStmt();
if (CurTok == tok_endcase || CurTok == tok_otherwise) {
fprintf(stderr, "error: Unexpected CASE terminator at line %d\n", Lex.getLine());
diff --git a/lib/Parser/ParserExpression.cc b/lib/Parser/ParserExpression.cc
index 96132aa..ee3e193 100644
--- a/lib/Parser/ParserExpression.cc
+++ b/lib/Parser/ParserExpression.cc
@@ -109,13 +109,13 @@ bool Parser::ParseDesignatorSuffix(std::vector<DesignatorAccess> &Out) {
Out.push_back(std::move(A));
} else if (CurTok == '.') {
getNextToken();
- if (CurTok != tok_identifier && CurTok != tok_new) {
+ if (CurTok != tok_identifier && CurTok != tok_new && CurTok != tok_delete) {
fprintf(stderr, "error: Expected field name after '.' at line %d\n", Lex.getLine());
return false;
}
DesignatorAccess A;
A.Kind = AccessKind::Field;
- A.FieldName = CurTok == tok_new ? "NEW" : Lex.IdentifierStr;
+ A.FieldName = CurTok == tok_new ? "NEW" : CurTok == tok_delete ? "DELETE" : Lex.IdentifierStr;
Out.push_back(std::move(A));
getNextToken();
} else if (CurTok == '^') {
diff --git a/lib/Parser/ParserFunction.cc b/lib/Parser/ParserFunction.cc
index 1b8b470..e4e635e 100644
--- a/lib/Parser/ParserFunction.cc
+++ b/lib/Parser/ParserFunction.cc
@@ -132,6 +132,10 @@ std::optional<std::vector<ParamDecl>> Parser::ParsePrototypeArgs(bool AllowByRef
std::unique_ptr<FunctionDefAST> Parser::ParseFunction(const std::string &OwnerClass, bool IsPublic) {
int Line = Lex.getLine();
getNextToken();
+ if (CurTok == tok_delete || (CurTok == tok_identifier && sameIdentifier(Lex.IdentifierStr, "DELETE"))) {
+ fprintf(stderr, "error: destructor DELETE must be a PROCEDURE at line %d\n", Line);
+ return nullptr;
+ }
if (CurTok == tok_new || (CurTok == tok_identifier && sameIdentifier(Lex.IdentifierStr, "NEW"))) {
fprintf(stderr, "error: constructor NEW must be a PROCEDURE at line %d\n", Line);
return nullptr;
@@ -180,12 +184,16 @@ std::unique_ptr<FunctionDefAST> Parser::ParseFunction(const std::string &OwnerCl
std::unique_ptr<FunctionDefAST> Parser::ParseProcedure(const std::string &OwnerClass, bool IsPublic) {
int Line = Lex.getLine();
getNextToken();
- if (CurTok != tok_identifier && CurTok != tok_new) {
+ if (CurTok != tok_identifier && CurTok != tok_new && CurTok != tok_delete) {
fprintf(stderr, "error: Expected procedure name\n");
return nullptr;
}
- std::string Name = CurTok == tok_new ? "NEW" : Lex.IdentifierStr;
+ std::string Name = CurTok == tok_new ? "NEW" : CurTok == tok_delete ? "DELETE" : Lex.IdentifierStr;
+ if (OwnerClass.empty() && sameIdentifier(Name, "DELETE")) {
+ fprintf(stderr, "error: destructor DELETE is only allowed in a CLASS at line %d\n", Line);
+ return nullptr;
+ }
if (OwnerClass.empty() && sameIdentifier(Name, "NEW")) {
fprintf(stderr, "error: constructor NEW is only allowed in a CLASS at line %d\n", Line);
return nullptr;
@@ -194,6 +202,10 @@ std::unique_ptr<FunctionDefAST> Parser::ParseProcedure(const std::string &OwnerC
auto Args = ParsePrototypeArgs(/*AllowByRef=*/true);
if (!Args) return nullptr;
+ if (sameIdentifier(Name, "DELETE") && !Args->empty()) {
+ fprintf(stderr, "error: destructor DELETE must have no parameters at line %d\n", Line);
+ return nullptr;
+ }
auto Proto = std::make_unique<PrototypeAST>(Name, std::move(*Args), "VOID", false, OwnerClass, IsPublic);
std::vector<std::unique_ptr<StmtAST>> Body = ParseBlock({tok_endprocedure});
diff --git a/tests/iii/class-destructor-duplicate-error/case.json b/tests/iii/class-destructor-duplicate-error/case.json
new file mode 100644
index 0000000..3ffc750
--- /dev/null
+++ b/tests/iii/class-destructor-duplicate-error/case.json
@@ -0,0 +1,8 @@
+{
+ "errors": [
+ {
+ "reason": "Duplicate member delete in class Box",
+ "line": 4
+ }
+ ]
+}
diff --git a/tests/iii/class-destructor-duplicate-error/program.cps b/tests/iii/class-destructor-duplicate-error/program.cps
new file mode 100644
index 0000000..661a9c2
--- /dev/null
+++ b/tests/iii/class-destructor-duplicate-error/program.cps
@@ -0,0 +1,6 @@
+CLASS Box
+PROCEDURE DELETE()
+ENDPROCEDURE
+PROCEDURE delete()
+ENDPROCEDURE
+ENDCLASS
diff --git a/tests/iii/class-destructor-function-error/case.json b/tests/iii/class-destructor-function-error/case.json
new file mode 100644
index 0000000..5482f9d
--- /dev/null
+++ b/tests/iii/class-destructor-function-error/case.json
@@ -0,0 +1,8 @@
+{
+ "errors": [
+ {
+ "reason": "destructor DELETE must be a PROCEDURE",
+ "line": 2
+ }
+ ]
+}
diff --git a/tests/iii/class-destructor-function-error/program.cps b/tests/iii/class-destructor-function-error/program.cps
new file mode 100644
index 0000000..89a2b5a
--- /dev/null
+++ b/tests/iii/class-destructor-function-error/program.cps
@@ -0,0 +1,5 @@
+CLASS Box
+FUNCTION DELETE() RETURNS INTEGER
+RETURN 0
+ENDFUNCTION
+ENDCLASS
diff --git a/tests/iii/class-destructor-global-error/case.json b/tests/iii/class-destructor-global-error/case.json
new file mode 100644
index 0000000..9df30b7
--- /dev/null
+++ b/tests/iii/class-destructor-global-error/case.json
@@ -0,0 +1,8 @@
+{
+ "errors": [
+ {
+ "reason": "destructor DELETE is only allowed in a CLASS",
+ "line": 1
+ }
+ ]
+}
diff --git a/tests/iii/class-destructor-global-error/program.cps b/tests/iii/class-destructor-global-error/program.cps
new file mode 100644
index 0000000..3bf448c
--- /dev/null
+++ b/tests/iii/class-destructor-global-error/program.cps
@@ -0,0 +1,2 @@
+PROCEDURE DELETE()
+ENDPROCEDURE
diff --git a/tests/iii/class-destructor-parameters-error/case.json b/tests/iii/class-destructor-parameters-error/case.json
new file mode 100644
index 0000000..6b2f839
--- /dev/null
+++ b/tests/iii/class-destructor-parameters-error/case.json
@@ -0,0 +1,8 @@
+{
+ "errors": [
+ {
+ "reason": "destructor DELETE must have no parameters",
+ "line": 2
+ }
+ ]
+}
diff --git a/tests/iii/class-destructor-parameters-error/program.cps b/tests/iii/class-destructor-parameters-error/program.cps
new file mode 100644
index 0000000..a3d9fd7
--- /dev/null
+++ b/tests/iii/class-destructor-parameters-error/program.cps
@@ -0,0 +1,4 @@
+CLASS Box
+PROCEDURE DELETE(x : INTEGER)
+ENDPROCEDURE
+ENDCLASS