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 #38

Merged
merged 2 commits into from
Jan 25, 2025
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
79 changes: 6 additions & 73 deletions examples/main.ll
Original file line number Diff line number Diff line change
@@ -1,76 +1,9 @@
>> "start: main.ll";

func counter() -> func -> void {
var i -> number := 0;
func count() -> void {
i := i + 1;
>> i;
<> Fibonacci
func fib(n -> number) -> number {
if (n < 2) {
return n;
}

return count;
return fib(n - 1) + fib(n - 2);
}
>> fib(28);

var cc -> func -> void;
var dd -> func -> void;
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, I am" >> name;
}
}

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

func print() -> void {
>> "Hi, I am a student, my name is" >> name;
}
}

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();
7 changes: 6 additions & 1 deletion examples/refactor.ll
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ class person {

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

if (x > -12) {
var y -> number := 10;
dd := counter();
cc();
dd();
}
var lout -> func -> void := p.print;
lout();
7 changes: 5 additions & 2 deletions grammar_rules
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ class_declaration : class identifier : identifier '{' ( variable_declaration
variable_declaration : 'var' identifier '->' type := expression
| 'var' identifier '->' type;

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

parameters : identifier -> type ( COMMA identifier -> type )*
arguments : expression ( COMMA expression )*

type : 'string' | 'number' | void | identifier | '(' ')' -> type?
output_stream : >> output output_stream*
Expand Down Expand Up @@ -57,5 +60,5 @@ factor : (PLUS|MINUS)factor

property_chain : (identifier_or_call) ( . property_chain )*
identifier_or_call : identifier | call
call : identifier '(' ')'
call : identifier '(' arguments? ')'
identifier : IDENTIFIER
40 changes: 24 additions & 16 deletions src/ast/ast.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,17 +95,6 @@ class ClassDeclarationAST : public AST {
std::vector<FunctionDeclarationAST *> methods;
};

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

std::stack<TypeAST*> types;
Token identifier;
StatementListAST *statements;
};

class OutputStreamAST : public AST {
public:
Expand All @@ -126,14 +115,33 @@ class InputStreamAST : public AST {

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

std::stack<TypeAST*> types;
TypeAST* type;
Token identifier;
AST *value;
};
class FunctionDeclarationAST : public AST {
public:
FunctionDeclarationAST(Token identifier, TypeAST *type,
std::vector<VariableDeclarationAST *> parameters,
StatementListAST *statements)
: identifier(identifier), type(type), parameters(parameters), statements(statements) {}
ASTValue *accept(ASTVisitor &visitor) override;
std::vector<std::string> getParameterNames() {
std::vector<std::string> names;
for (auto &parameter : parameters)
names.push_back(parameter->identifier.getValue());
return names;
}

TypeAST *type;
std::vector<VariableDeclarationAST *> parameters;
Token identifier;
StatementListAST *statements;
};

class AssignmentVariableAST : public AST {
public:
Expand Down Expand Up @@ -185,10 +193,10 @@ class PropertyChainAST : public AST {

class TypeAST : public AST {
public:
TypeAST(Token token) : token(token) {}
TypeAST(std::stack<IdentifierAST*> types) : types(types) {}
ASTValue *accept(ASTVisitor &visitor) override;

Token token;
std::stack<IdentifierAST*> types;
};

class IdentifierAST : public AST {
Expand Down
71 changes: 43 additions & 28 deletions src/ast/interpreter_visitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,22 +95,18 @@ ASTValue *InterpreterVisitor::visitClassDeclaration(ClassDeclarationAST *expr) {

ASTValue *
InterpreterVisitor::visitFunctionDeclaration(FunctionDeclarationAST *expr) {
ASTValue *type = expr->type->accept(*this);
std::vector<LangObject *> parameters;
for (auto &parameter : expr->parameters)
parameters.push_back(parameter->type->accept(*this)->value);

ASTValue *type = nullptr;
std::stack<TypeAST *> types = expr->types;
while (!types.empty()) {
ASTValue *t = types.top()->accept(*this);
if (type == nullptr)
type = t;
else
type = new ASTValue(new LangFunction(nullptr, type->value, this->scope));
types.pop();
}
/*for (auto &parameter : expr->parameters)*/
/* parameter->accept(*this);*/

FuncSymbol *func =
new FuncSymbol(expr->identifier.getValue(),
new ASTValue(new LangFunction(expr->statements,
type->value, this->scope)));
FuncSymbol *func = new FuncSymbol(
expr->identifier.getValue(),
new ASTValue(new LangFunction(expr->statements, parameters, expr->parameters,
type->value, this->scope)));
this->scope->set(func);
return new ASTValue(new LangNil());
}
Expand All @@ -136,17 +132,8 @@ ASTValue *InterpreterVisitor::visitInputStream(InputStreamAST *expr) {
ASTValue *
InterpreterVisitor::visitVariableDeclaration(VariableDeclarationAST *expr) {
ASTValue *value = expr->value->accept(*this);
ASTValue *type = expr->type->accept(*this);

ASTValue *type = nullptr;
std::stack<TypeAST *> types = expr->types;
while (!types.empty()) {
ASTValue *t = types.top()->accept(*this);
if (type == nullptr)
type = t;
else
type = new ASTValue(new LangFunction(nullptr, type->value, this->scope));
types.pop();
}
if (dynamic_cast<LangNil *>(value->value) != nullptr)
value->value = new LangNil(type->value);
this->scope->set(new VarSymbol(expr->identifier.getValue(), value));
Expand Down Expand Up @@ -218,11 +205,18 @@ ASTValue *InterpreterVisitor::visitUnaryOperatorExpr(UnaryOperatorAST *expr) {
throw RuntimeError("invalid operator: " + expr->op.getValue(), expr->op);
}

ASTValue *InterpreterVisitor::visitCall(LangObject *callee, std::string name, Token &token) {
ASTValue *InterpreterVisitor::visitCall(LangObject *callee, std::string name, std::vector<LangObject *> arguments,
Token &token) {
if (typeid(*callee) == typeid(LangFunction)) {
LangFunction *func = dynamic_cast<LangFunction *>(callee);
ScopedSymbolTable *currentScope = this->scope;
this->scope = func->getScope()->newScope("functioncall");
for (int i = 0; i < func->getParameters().size(); i++) {
std::string name = func->getParameters()[i]->identifier.getValue();
ASTValue *value = new ASTValue(arguments[i]);
this->scope->set(new VarSymbol(name, value));
}

LangObject *returnValue = new LangVoid();
try {
func->getValue()->accept(*this);
Expand All @@ -242,7 +236,12 @@ ASTValue *InterpreterVisitor::visitCall(LangObject *callee, std::string name, To
ASTValue *InterpreterVisitor::visitCall(CallAST *expr) {
ASTValue *value =
this->scope->getValue(expr->identifier.getValue(), this->jumpTable[expr]);
return this->visitCall(value->value, expr->identifier.getValue(), expr->identifier);
std::vector<LangObject *> arguments;
for (auto &argument : expr->arguments)
arguments.push_back(argument->accept(*this)->value);

return this->visitCall(value->value, expr->identifier.getValue(), arguments,
expr->identifier);
}

ASTValue *InterpreterVisitor::visitPropertyChain(PropertyChainAST *expr) {
Expand All @@ -258,14 +257,30 @@ ASTValue *InterpreterVisitor::visitPropertyChain(PropertyChainAST *expr) {
} else if (typeid(*node) == typeid(CallAST)) {
CallAST *call = dynamic_cast<CallAST *>(node);
value = instance->getScope()->getValue(call->identifier.getValue(), 0);
value = this->visitCall(value->value, call->identifier.getValue(), call->identifier);
std::vector<LangObject *> arguments;
for (auto &argument : call->arguments)
arguments.push_back(argument->accept(*this)->value);

value = this->visitCall(value->value, call->identifier.getValue(), arguments,
call->identifier);
}
}
return value;
}

ASTValue *InterpreterVisitor::visitType(TypeAST *expr) {
return this->scope->getValue(expr->token.getValue(), this->jumpTable[expr]);
ASTValue *type = nullptr;
std::stack<IdentifierAST *> types = expr->types;
while (!types.empty()) {
ASTValue *t = types.top()->accept(*this);
if (type == nullptr)
type = t;
else
type =
new ASTValue(new LangFunction(nullptr, {}, {}, type->value, this->scope));
types.pop();
}
return type;
}

ASTValue *InterpreterVisitor::visitIdentifier(IdentifierAST *expr) {
Expand Down
56 changes: 29 additions & 27 deletions src/ast/interpreter_visitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,61 +5,63 @@
#include "visitor.h"
#include <iostream>

class InterpreterVisitor: public ASTVisitor {
class InterpreterVisitor : public ASTVisitor {
public:
static void setJumpTable(std::unordered_map<AST*, int> jumpTable) {
static void setJumpTable(std::unordered_map<AST *, int> jumpTable) {
InterpreterVisitor::jumpTable = jumpTable;
}

protected:
private:
static ScopedSymbolTable *scope;
static std::unordered_map<AST*, int> jumpTable;
static std::unordered_map<AST *, int> jumpTable;

ASTValue *visitCall(LangObject *callee, std::string name,
std::vector<LangObject *> arguments, Token &token);

ASTValue *visitCall(LangObject *callee, std::string name, Token &token);
ASTValue *visitStatementList(StatementListAST *expr) override;

ASTValue* visitStatementList(StatementListAST *expr) override;
ASTValue *visitBLock(BlockAST *expr) override;

ASTValue* visitBLock(BlockAST *expr) override;
ASTValue *visitReturn(ReturnAST *expr) override;

ASTValue* visitReturn(ReturnAST *expr) override;
ASTValue *visitWhileStatement(WhileStatementAST *expr) override;

ASTValue* visitWhileStatement(WhileStatementAST *expr) override;
ASTValue *visitForStatement(ForStatementAST *expr) override;

ASTValue* visitForStatement(ForStatementAST *expr) override;
ASTValue *visitIfStatement(IfStatementAST *expr) override;

ASTValue* visitIfStatement(IfStatementAST *expr) override;
ASTValue *visitClassDeclaration(ClassDeclarationAST *expr) override;

ASTValue* visitClassDeclaration(ClassDeclarationAST *expr) override;
ASTValue *visitFunctionDeclaration(FunctionDeclarationAST *expr) override;

ASTValue* visitFunctionDeclaration(FunctionDeclarationAST *expr) override;
ASTValue *visitOutputStream(OutputStreamAST *expr) override;

ASTValue* visitOutputStream(OutputStreamAST *expr) override;
ASTValue *visitInputStream(InputStreamAST *expr) override;

ASTValue* visitInputStream(InputStreamAST *expr) override;

ASTValue* visitVariableDeclaration(VariableDeclarationAST *expr) override;
ASTValue *visitVariableDeclaration(VariableDeclarationAST *expr) override;

ASTValue* visitAssignmentVariable(AssignmentVariableAST *expr) override;
ASTValue *visitAssignmentVariable(AssignmentVariableAST *expr) override;

ASTValue* visitBinaryOperatorExpr(BinaryOperatorAST *expr) override;
ASTValue *visitBinaryOperatorExpr(BinaryOperatorAST *expr) override;

ASTValue* visitUnaryOperatorExpr(UnaryOperatorAST *expr) override;
ASTValue *visitUnaryOperatorExpr(UnaryOperatorAST *expr) override;

ASTValue* visitCall(CallAST *expr) override;
ASTValue *visitCall(CallAST *expr) override;

ASTValue* visitPropertyChain(PropertyChainAST *expr) override;
ASTValue *visitPropertyChain(PropertyChainAST *expr) override;

ASTValue* visitType(TypeAST* expr) override;
ASTValue *visitType(TypeAST *expr) override;

ASTValue* visitIdentifier(IdentifierAST *expr) override;
ASTValue *visitIdentifier(IdentifierAST *expr) override;

ASTValue* visitNumberExpr(NumberAST *expr) override;
ASTValue *visitNumberExpr(NumberAST *expr) override;

ASTValue* visitStringExpr(StringAST *expr) override;
ASTValue *visitStringExpr(StringAST *expr) override;

ASTValue* visitVoid(VoidAST *expr) override;
ASTValue *visitVoid(VoidAST *expr) override;

ASTValue* visitNil(NilAST *expr) override;
ASTValue *visitNil(NilAST *expr) override;
};

#endif // INTERPRETER_VISITOR_H
Loading
Loading