Skip to content
This repository has been archived by the owner on Mar 12, 2023. It is now read-only.

Commit

Permalink
-resolved #21
Browse files Browse the repository at this point in the history
-resolved #10
-resolved #8
  • Loading branch information
aczwink committed Dec 27, 2020
1 parent 9274040 commit 8b469ec
Show file tree
Hide file tree
Showing 68 changed files with 1,329 additions and 253 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/cmake-build-debug/
/.idea/
3 changes: 3 additions & 0 deletions acs_lib/general.acs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@ extern *: (float64, float64) -> float64;
//logical operators
extern <: (float64, float64) -> bool;
extern <=: (float64, float64) -> bool;
extern =: (float64, float64) -> bool;
extern and: (bool, bool) -> bool;
extern not: bool -> bool;
extern or: (bool, bool) -> bool;

//general purpose
extern []: (float64, float64) -> float64; //TODO: make this a generic, make this work with ...
extern __set: (any, string, any) -> any; //TODO: this is ugly
extern print: any -> null;
8 changes: 8 additions & 0 deletions examples/05_fib_linear.acs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
let helper := {
(current_n, n, a, _) | current_n = n -> a,
(current_n, n, a, b) -> self( (current_n + 1, n, b, a + b) )
};

let fib := n -> helper( (0, n, 0, 1) );

print(fib(30));
30 changes: 30 additions & 0 deletions examples/06_objects.acs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*let square := x -> x * x;

let test := square(5);
print(test);*/



let new_vec := {
x: 5,
y: 5,

test: { x, y } -> x * y
};
print(new_vec);
//print(new_vec.test());



/*
let new_vec := (x, y) -> {
x: x,
y: y,

*: ( { x, y }, scaleFactor) -> new_vec( (x * scaleFactor, y * scaleFactor) )
};

let vec := new_vec( (5, 5) );

let vecTest := square(vec);
print(vecTest);*/
17 changes: 0 additions & 17 deletions examples/objects.acs

This file was deleted.

5 changes: 4 additions & 1 deletion include/acsb/Opcode.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ enum class Opcode : uint8
CallExtern,
JumpOnFalse,
LoadConstant,
NewDictionary,
NewTuple,
PushParameter,
Pop,
PopAssign,
Push,
Return,
};
4 changes: 3 additions & 1 deletion parser/lexer.l
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ extern AST::ParserState* g_parserState;
%%
[ \t\r\n] ; //ignore whitespaces
"//".* {} //skip comments
[/][*][^*]*[*]+([^*/][^*]*[*]+)*[/] {} //skip comments

"," { return TOKEN_COMMA; }
":" { return TOKEN_COLON; }
Expand All @@ -43,10 +44,11 @@ extern AST::ParserState* g_parserState;

"+" { return TOKEN_PLUS; }
"-" { return TOKEN_MINUS; }
"=" { return TOKEN_EQUALS; }

"extern" { return TOKEN_KEYWORD_EXTERN; }
"let" { return TOKEN_KEYWORD_LET; }

[0-9]+ { yylval.str = g_parserState->CreateString(yytext); return TOKEN_NATURAL_LITERAL; }
[a-zA-Z0-9\-\*<=]+ { yylval.str = g_parserState->CreateString(yytext); return TOKEN_IDENTIFIER; }
[a-zA-Z0-9\-\*<=_\[\]]+ { yylval.str = g_parserState->CreateString(yytext); return TOKEN_IDENTIFIER; }
%%
144 changes: 77 additions & 67 deletions parser/parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ void yyerror(AST::ParserState* parserState, const char* s);

%token TOKEN_PLUS
%token TOKEN_MINUS
%token TOKEN_EQUALS

%token TOKEN_KEYWORD_EXTERN
%token TOKEN_KEYWORD_LET
Expand All @@ -48,16 +49,14 @@ void yyerror(AST::ParserState* parserState, const char* s);
%start module


%left TOKEN_PAREN_OPEN



%union
{
String* str;

AST::Expression* expr;
AST::FunctionExpression* funcExpr;
AST::ObjectExpression* objExpr;
AST::ExpressionList* exprList;

AST::LeftValue* lvalue;
Expand All @@ -72,125 +71,136 @@ void yyerror(AST::ParserState* parserState, const char* s);
%type <str> function_name

%type <expr> expression
%type <expr> expression_without_function_value
%type <expr> value_expression
%type <expr> function_call
%type <expr> function_expression
%type <funcExpr> functionRules
%type <expr> pattern
%type <expr> tuple_expression
%type <exprList> tuple_expressions

%type <funcExpr> function_rule_leaf
%type <funcExpr> function_rules

%type <objExpr> object_entry
%type <objExpr> object_entries

%type <exprList> tuple_entries

%type <stmt> statement

%type <type> typespec
%type <type> function_type
%type <type> tuple_type
%type <tupleTypeSpec> tuple_type
%type <tupleTypeSpec> tuple_type_entries

%type <lvalue> left_value

%parse-param { AST::ParserState* parserState }

%%
%right infixprec
%left TOKEN_IDENTIFIER
%left TOKEN_MAP

%%
module:
statement { parserState->ModuleStatements().AddStatement($1); }
| statement module { parserState->ModuleStatements().AddStatement($1); }
statement { parserState->ModuleStatements().AddStatement($1); }
| statement module { parserState->ModuleStatements().AddStatement($1); }
;

statement:
TOKEN_KEYWORD_EXTERN function_name TOKEN_COLON typespec TOKEN_SEMICOLON { $$ = new AST::ExternalDeclarationStatement(*$2, $4); }
| function_call TOKEN_SEMICOLON { $$ = new AST::ExpressionStatement($1); }
| TOKEN_KEYWORD_LET left_value TOKEN_ASSIGN expression TOKEN_SEMICOLON { $$ = new AST::VariableDefinitionStatement($2, $4); }
| expression TOKEN_SEMICOLON { $$ = new AST::ExpressionStatement($1); }
;





typespec:
TOKEN_IDENTIFIER { $$ = new AST::IdentifierTypeSpec(*$1); }
| function_type { $$ = $1; }
| tuple_type { $$ = $1; }
function_name:
TOKEN_IDENTIFIER { $$ = $1; }
| TOKEN_PLUS { $$ = parserState->CreateString(u8"+"); }
| TOKEN_MINUS { $$ = parserState->CreateString(u8"-"); }
| TOKEN_EQUALS { $$ = parserState->CreateString(u8"="); }
;

function_type:
typespec TOKEN_MAP typespec { $$ = new AST::FunctionTypeSpec($1, $3); }
left_value:
TOKEN_IDENTIFIER { $$ = new AST::IdentifierLeftValue(*$1); }
;

tuple_type_entries:
typespec { $$ = new AST::TupleTypeSpec($1); }
| typespec TOKEN_MORE { $$ = new AST::TupleTypeSpec($1, true); }
| typespec TOKEN_COMMA tuple_type_entries { $$ = $3; $3->AddTypeSpec($1); }
;

tuple_type:
TOKEN_PAREN_OPEN tuple_type_entries TOKEN_PAREN_CLOSE { $$ = $2; }
;

expression:
function_rule_leaf { $$ = $1; }
| TOKEN_BRACE_OPEN function_rules TOKEN_BRACE_CLOSE { $$ = $2; }
| expression_without_function_value { $$ = $1; }
;

expression_without_function_value:
TOKEN_IDENTIFIER TOKEN_PAREN_OPEN expression TOKEN_PAREN_CLOSE { $$ = new AST::CallExpression(*$1, $3); }
| value_expression { $$ = $1; }

//infix
| expression_without_function_value TOKEN_IDENTIFIER expression_without_function_value { $$ = new AST::CallExpression(*$2, new AST::TupleExpression(new AST::ExpressionList($1, $3))); }
| expression_without_function_value TOKEN_PLUS expression_without_function_value { $$ = new AST::CallExpression(u8"+", new AST::TupleExpression(new AST::ExpressionList($1, $3))); }
| expression_without_function_value TOKEN_EQUALS expression_without_function_value { $$ = new AST::CallExpression(u8"=", new AST::TupleExpression(new AST::ExpressionList($1, $3))); }
;


function_name:
TOKEN_IDENTIFIER { $$ = $1; }
| TOKEN_PLUS { $$ = parserState->CreateString(u8"+"); }
| TOKEN_MINUS { $$ = parserState->CreateString(u8"-"); }
value_expression:
TOKEN_PAREN_OPEN tuple_entries TOKEN_PAREN_CLOSE { $$ = new AST::TupleExpression($2); }
| TOKEN_BRACE_OPEN object_entries TOKEN_BRACE_CLOSE { $$ = $2; }
| TOKEN_NATURAL_LITERAL { $$ = new AST::NaturalLiteralExpression(*$1); }
| TOKEN_IDENTIFIER { $$ = new AST::IdentifierExpression(*$1); }
;


function_rule_leaf:
value_expression TOKEN_MAP expression_without_function_value { $$ = new AST::FunctionExpression($1, nullptr, $3); }
| value_expression TOKEN_GUARD expression_without_function_value TOKEN_MAP expression_without_function_value { $$ = new AST::FunctionExpression($1, $3, $5); }
;

function_call:
TOKEN_IDENTIFIER TOKEN_PAREN_OPEN expression TOKEN_PAREN_CLOSE { $$ = new AST::CallExpression(*$1, $3); }
function_rules:
function_rule_leaf { $$ = $1; }
| function_rule_leaf TOKEN_COMMA function_rules { $$ = $3; $3->CombineRulesAndPrepend($1); }
;


tuple_entries:
expression { $$ = new AST::ExpressionList($1); }
| expression TOKEN_COMMA tuple_entries { $3->InsertAtBeginning($1); $$ = $3; }
;


expression:
value_expression { $$ = $1; }
| TOKEN_IDENTIFIER { $$ = new AST::IdentifierExpression(*$1); }
| function_call { $$ = $1; }
| expression function_name expression /*infix notation call*/ { $$ = new AST::CallExpression(*$2, new AST::TupleExpression(new AST::ExpressionList($1, $3))); }
object_entry:
function_name { $$ = new AST::ObjectExpression(*$1); }
| function_name TOKEN_COLON expression { $$ = new AST::ObjectExpression(*$1, $3); }
;

value_expression:
TOKEN_NATURAL_LITERAL { $$ = new AST::NaturalLiteralExpression(*$1); }
| function_expression { $$ = $1; }
| tuple_expression { $$ = $1; }
object_entries:
object_entry { $$ = $1; }
| object_entry TOKEN_COMMA object_entries { $$ = $3; $3->AddMember($1); }
;


function_expression:
pattern TOKEN_MAP expression { $$ = new AST::FunctionExpression($1, nullptr, $3); }
| TOKEN_BRACE_OPEN functionRules TOKEN_BRACE_CLOSE { $$ = $2; }
;

functionRules:
pattern TOKEN_MAP expression { $$ = new AST::FunctionExpression($1, nullptr, $3); }
| pattern TOKEN_MAP expression TOKEN_COMMA functionRules { $$ = $5; $5->AddRule($1, nullptr, $3); }
| pattern TOKEN_GUARD expression TOKEN_MAP expression TOKEN_COMMA functionRules { $$ = $7; $7->AddRule($1, $3, $5); }
;


tuple_expressions:
expression { $$ = new AST::ExpressionList($1); }
| expression TOKEN_COMMA tuple_expressions { $3->InsertAtBeginning($1); $$ = $3; }
;

tuple_expression:
TOKEN_PAREN_OPEN tuple_expressions TOKEN_PAREN_CLOSE { $$ = new AST::TupleExpression($2); }
;



typespec:
TOKEN_IDENTIFIER { $$ = new AST::IdentifierTypeSpec(*$1); }
| function_type { $$ = $1; }
| tuple_type { $$ = $1; }
;

left_value:
TOKEN_IDENTIFIER { $$ = new AST::IdentifierLeftValue(*$1); }
function_type:
typespec TOKEN_MAP typespec { $$ = new AST::FunctionTypeSpec($1, $3); }
;

pattern:
TOKEN_NATURAL_LITERAL { $$ = new AST::NaturalLiteralExpression(*$1); }
| TOKEN_IDENTIFIER { $$ = new AST::IdentifierExpression(*$1); }
tuple_type_entries:
typespec { $$ = new AST::TupleTypeSpec($1); }
| typespec TOKEN_MORE { $$ = new AST::TupleTypeSpec($1, true); }
| typespec TOKEN_COMMA tuple_type_entries { $$ = $3; $3->AddTypeSpec($1); }
;

tuple_type:
TOKEN_PAREN_OPEN tuple_type_entries TOKEN_PAREN_CLOSE { $$ = $2; }
;

%%
23 changes: 0 additions & 23 deletions src/acsb/Compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,6 @@ void ACSB::Compiler::CompileCall(const IR::CallOperation & op)

this->executionStack.Pop();
this->executionStack.Pop();
this->executionStack.Pop();
this->executionStack.Push(&op.GetValue());
}

void Compiler::CompileCreateArray(const IR::CreateArrayOperation & op)
{
this->AddInstruction(Opcode::CreateArray);
this->executionStack.Push(&op.GetValue());
}

void Compiler::CompileStore(const IR::StoreOperation& op)
Expand Down Expand Up @@ -120,21 +112,6 @@ void ACSB::Compiler::CompileSymbol(const IR::Symbol & symbol)
this->AddInstruction(Opcode::PushProc, symbol.GetNumber());
}
break;
case IR::SymbolType::Temporary:
{
uint32 index = Unsigned<uint32>::Max();
for (int32 idx = this->executionStack.GetNumberOfElements() - 1; idx >= 0; idx--)
{
if (*this->executionStack[idx] == symbol)
{
index = idx;
break;
}
}

this->AddInstruction(Opcode::Push, index);
}
break;
case IR::SymbolType::This:
this->opcodes.Push(Opcode::PushThis);
break;
Expand Down
2 changes: 0 additions & 2 deletions src/acsb/Compiler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ namespace ACSB
void BeginProcedure() override;
void CompileAccess(const IR::AccessOperation & op) override;
void CompileCall(const IR::CallOperation & op) override;
void CompileCreateArray(const IR::CreateArrayOperation & op) override;
void CompileStore(const IR::StoreOperation& op) override;
void CompileWait(const IR::WaitOperation & op) override;
void EndProcedure() override;
Expand All @@ -55,7 +54,6 @@ namespace ACSB
Map<String, uint32> localMap;
Map<String, uint32> globalMap;
uint16 nextLocalIdx;
DynamicArray<const IR::Symbol*> executionStack;
uint32 lastProcOffset;

//Methods
Expand Down
Loading

0 comments on commit 8b469ec

Please sign in to comment.