Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Develop #34

Merged
merged 5 commits into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 61 additions & 9 deletions examples/main.ll
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
var i -> number := 0;

func add() -> number {
return 1+1;
}
var r -> number := 12;
r := 23;
>> "start: main.ll";

func counter() -> func -> void {
var i -> number := 0;
Expand All @@ -15,10 +9,68 @@ func counter() -> func -> void {

return count;
}

var cc -> func -> void;
var dd -> func -> func -> void;
var dd -> func -> void;
counter();
dd := counter();
cc := counter();
dd := cc;
cc();
cc();
cc();
dd();
dd();
dd();

var x -> number := 5;

class address {
var street -> string := "123 Main St";
var city -> string := "Springfield";
var state -> string := "IL";
var zip -> string := "62704";

func print() -> void {
>> street;
>> city;
>> state;
>> zip;
}
}

class person {
var age -> number := 12 + x;
var name -> string := "John";
var addr -> address := address();

func print() -> void {
>> "Hello";
}
}

class student : person {
var code -> number := 35;

func print() -> void {
>> "Student";
}
}

func newperson() -> person {
var p -> person := person();
>> p.age;
>> p.name;
p.addr.print();
return p;
}
var person1 -> person := newperson();

var p -> person := person();
p.addr.print();

var lout -> func -> void := p.print;
lout();


var s -> student := student();
s.print();
38 changes: 26 additions & 12 deletions examples/refactor.ll
Original file line number Diff line number Diff line change
@@ -1,15 +1,29 @@
var cnt -> number;
var count -> number := 2;
var name -> string := "John";

func print() -> {
>> "Hello World!";
cnt := count := 0;
>> "Hello World!";
while(cnt < 10) {
cnt := cnt + 1;
>> "Name:" >> name;
func counter() -> func -> void {
var i -> number := 0;
func count() -> void {
i := i + 1;
>> i;
}

return count;
}
var cc -> func -> void;
var dd -> func -> void;
cc := counter();

var x -> number := 5;

class person {
var age -> number := 12 + x;
var name -> string := "John";

func print() -> void {
>> "Hello";
}
}

var p -> person := person();
p.print();

print();
var lout -> func -> void := p.print;
lout();
14 changes: 10 additions & 4 deletions grammar_rules
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ statement_list : statement
| statement statement_list

statement : variable_declaration SEMICOLON
| func_declaration
| class_declaration
| while_statement
| for_statement
| if_statement
| output_stream SEMICOLON
| input_stream SEMICOLON
| input_stream SEMICOLON
| expression
| identifier
| block
Expand All @@ -23,12 +25,13 @@ if_statement : 'if' '(' expression ')' '{' statement_list '}'
block : '{' statement_list '}'
return : 'return' expression ;

class_declaration : class identifier : identifier '{' ( variable_declaration | func_declaration )* '}'
variable_declaration : 'var' identifier '->' type := expression
| 'var' identifier '->' type;

func_declaration : 'func' identifier '(' ')' -> type? '{' statement_list '}'

type : 'string' | 'number' | void | '(' ')' -> type?
type : 'string' | 'number' | void | identifier | '(' ')' -> type?
output_stream : >> output output_stream*
output : expression

Expand All @@ -38,7 +41,7 @@ input_stream : << identifier inputStream*

expression : assignment

assignment : identifier := assignment
assignment : property_chain := assignment
| equality_expression (( == | != ) equality_expression)*


Expand All @@ -48,8 +51,11 @@ clause : term ( PLUS|MINUS ) term
term : factor ((DIV|MULT|MOD) factor)*
factor : (PLUS|MINUS)factor
| NUMBER | STRING
| identifier | call
| property_chain
| '(' expression ')'


property_chain : (identifier_or_call) ( . property_chain )*
identifier_or_call : identifier | call
call : identifier '(' ')'
identifier : IDENTIFIER
57 changes: 37 additions & 20 deletions src/ast/ast.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,53 +2,70 @@
#include "../error/error.h"
#include "visitor.h"

ASTValue* AST::accept(ASTVisitor &visitor) {
ASTValue *AST::accept(ASTVisitor &visitor) {
throw RuntimeError("not implemented");
}
ASTValue* StatementListAST::accept(ASTVisitor &visitor) {
ASTValue *StatementListAST::accept(ASTVisitor &visitor) {
return visitor.visitStatementList(this);
}
ASTValue* BlockAST::accept(ASTVisitor &visitor) { return visitor.visitBLock(this); }
ASTValue* ReturnAST::accept(ASTVisitor &visitor) { return visitor.visitReturn(this); }
ASTValue* WhileStatementAST::accept(ASTVisitor &visitor) {
ASTValue *BlockAST::accept(ASTVisitor &visitor) {
return visitor.visitBLock(this);
}
ASTValue *ReturnAST::accept(ASTVisitor &visitor) {
return visitor.visitReturn(this);
}
ASTValue *WhileStatementAST::accept(ASTVisitor &visitor) {
return visitor.visitWhileStatement(this);
}
ASTValue* ForStatementAST::accept(ASTVisitor &visitor) {
ASTValue *ForStatementAST::accept(ASTVisitor &visitor) {
return visitor.visitForStatement(this);
}
ASTValue* IfStatementAST::accept(ASTVisitor &visitor) {
ASTValue *IfStatementAST::accept(ASTVisitor &visitor) {
return visitor.visitIfStatement(this);
}
ASTValue* FunctionDeclarationAST::accept(ASTVisitor &visitor) {
ASTValue *ClassDeclarationAST::accept(ASTVisitor &visitor) {
return visitor.visitClassDeclaration(this);
}
ASTValue *FunctionDeclarationAST::accept(ASTVisitor &visitor) {
return visitor.visitFunctionDeclaration(this);
}
ASTValue* OutputStreamAST::accept(ASTVisitor &visitor) {
ASTValue *OutputStreamAST::accept(ASTVisitor &visitor) {
return visitor.visitOutputStream(this);
}
ASTValue* InputStreamAST::accept(ASTVisitor &visitor) {
ASTValue *InputStreamAST::accept(ASTVisitor &visitor) {
return visitor.visitInputStream(this);
}
ASTValue* VariableDeclarationAST::accept(ASTVisitor &visitor) {
ASTValue *VariableDeclarationAST::accept(ASTVisitor &visitor) {
return visitor.visitVariableDeclaration(this);
}
ASTValue* AssignmentVariableAST::accept(ASTVisitor &visitor) {
ASTValue *AssignmentVariableAST::accept(ASTVisitor &visitor) {
return visitor.visitAssignmentVariable(this);
}
ASTValue* BinaryOperatorAST::accept(ASTVisitor &visitor) {
ASTValue *BinaryOperatorAST::accept(ASTVisitor &visitor) {
return visitor.visitBinaryOperatorExpr(this);
}
ASTValue* UnaryOperatorAST::accept(ASTVisitor &visitor) {
ASTValue *UnaryOperatorAST::accept(ASTVisitor &visitor) {
return visitor.visitUnaryOperatorExpr(this);
}
ASTValue* CallAST::accept(ASTVisitor &visitor) { return visitor.visitCall(this); }
ASTValue* IdentifierAST::accept(ASTVisitor &visitor) {
ASTValue *CallAST::accept(ASTVisitor &visitor) {
return visitor.visitCall(this);
}
ASTValue *PropertyChainAST::accept(ASTVisitor &visitor) {
return visitor.visitPropertyChain(this);
}
ASTValue *TypeAST::accept(ASTVisitor &visitor) {
return visitor.visitType(this);
}
ASTValue *IdentifierAST::accept(ASTVisitor &visitor) {
return visitor.visitIdentifier(this);
}
ASTValue* NumberAST::accept(ASTVisitor &visitor) {
ASTValue *NumberAST::accept(ASTVisitor &visitor) {
return visitor.visitNumberExpr(this);
}
ASTValue* StringAST::accept(ASTVisitor &visitor) {
ASTValue *StringAST::accept(ASTVisitor &visitor) {
return visitor.visitStringExpr(this);
}
ASTValue* VoidAST::accept(ASTVisitor &visitor) { return visitor.visitVoid(this); }
ASTValue* NilAST::accept(ASTVisitor &visitor) { return visitor.visitNil(this); }
ASTValue *VoidAST::accept(ASTVisitor &visitor) {
return visitor.visitVoid(this);
}
ASTValue *NilAST::accept(ASTVisitor &visitor) { return visitor.visitNil(this); }
49 changes: 41 additions & 8 deletions src/ast/ast.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "visitor.h"
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <unordered_map>
Expand Down Expand Up @@ -79,14 +80,29 @@ class IfStatementAST : public AST {
StatementListAST *elseStatements;
};

class TypeAST;
class ClassDeclarationAST : public AST {
public:
ClassDeclarationAST(Token identifier, TypeAST *superclass,
std::vector<VariableDeclarationAST *> variables,
std::vector<FunctionDeclarationAST *> methods)
: identifier(identifier), superclass(superclass), variables(variables), methods(methods) {}

ASTValue *accept(ASTVisitor &visitor) override;
Token identifier;
TypeAST *superclass;
std::vector<VariableDeclarationAST *> variables;
std::vector<FunctionDeclarationAST *> methods;
};

class FunctionDeclarationAST : public AST {
public:
FunctionDeclarationAST(Token identifier, std::stack<Token> types,
FunctionDeclarationAST(Token identifier, std::stack<TypeAST*> types,
StatementListAST *statements)
: identifier(identifier), types(types), statements(statements) {}
ASTValue *accept(ASTVisitor &visitor) override;

std::stack<Token> types;
std::stack<TypeAST*> types;
Token identifier;
StatementListAST *statements;
};
Expand All @@ -101,30 +117,31 @@ class OutputStreamAST : public AST {

class InputStreamAST : public AST {
public:
InputStreamAST(std::vector<IdentifierAST> identifiers) : identifiers(identifiers) {}
InputStreamAST(std::vector<IdentifierAST> identifiers)
: identifiers(identifiers) {}
ASTValue *accept(ASTVisitor &visitor) override;

std::vector<IdentifierAST> identifiers;
};

class VariableDeclarationAST : public AST {
public:
VariableDeclarationAST(std::stack<Token> types, Token identifier, AST *value)
VariableDeclarationAST(std::stack<TypeAST*> types, Token identifier, AST *value)
: types(types), identifier(identifier), value(value) {}
ASTValue *accept(ASTVisitor &visitor) override;

std::stack<Token> types;
std::stack<TypeAST*> types;
Token identifier;
AST *value;
};

class AssignmentVariableAST : public AST {
public:
AssignmentVariableAST(Token identifier, AST *value)
: identifier(identifier), value(value) {}
AssignmentVariableAST(AST *leftReference, AST *value)
: leftReference(leftReference), value(value) {}
ASTValue *accept(ASTVisitor &visitor) override;

Token identifier;
AST *leftReference;
AST *value;
};

Expand Down Expand Up @@ -157,6 +174,22 @@ class CallAST : public AST {
std::vector<AST *> arguments;
};

class PropertyChainAST : public AST {
public:
PropertyChainAST(std::vector<AST *> accesses) : accesses(accesses) {}
ASTValue *accept(ASTVisitor &visitor) override;

std::vector<AST *> accesses;
};

class TypeAST : public AST {
public:
TypeAST(Token token) : token(token) {}
ASTValue *accept(ASTVisitor &visitor) override;

Token token;
};

class IdentifierAST : public AST {
public:
IdentifierAST(Token token) : token(token) {}
Expand Down
Loading