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

Commit

Permalink
worked on #19
Browse files Browse the repository at this point in the history
  • Loading branch information
aczwink committed Jan 17, 2021
1 parent 02d69b2 commit 01a2789
Show file tree
Hide file tree
Showing 51 changed files with 828 additions and 216 deletions.
43 changes: 24 additions & 19 deletions examples/06_objects.acs
Original file line number Diff line number Diff line change
@@ -1,30 +1,35 @@
/*let square := x -> x * x;

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

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

lengthSquared: null -> this.x * this.x + this.y * this.y

let new_vec := {
x: 5,
y: 5,
//+: ( this, { x, y } ) -> new_vec( ( this.x + x, this.y + y ) )

test: { x, y } -> x * y
/*
*: {
//( { x, y }, scaleFactor) | scaleFactor is float64 -> new_vec( (x * scaleFactor, y * scaleFactor) ),
( this, other ) -> new_vec( ( this.x * other.x, this.y * other.y ) )
}
*/
};
print(new_vec);
//print(new_vec.test());

let v1 := new_vec( (5, 5) );
print(v1);
print(v1.lengthSquared(null));

let v2 := new_vec( (10, 12) );
print(v2);
print(v2.lengthSquared(null));

/*
let new_vec := (x, y) -> {
x: x,
y: y,
//print(v1 + v2);

*: ( { x, y }, scaleFactor) -> new_vec( (x * scaleFactor, y * scaleFactor) )
};
/*
let square := x -> x * x;

let vec := new_vec( (5, 5) );
let test := square(5);
print(test);

let vecTest := square(vec);
print(vecTest);*/
print(vecTest);
*/
3 changes: 2 additions & 1 deletion include/acsb/Opcode.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2018-2020 Amir Czwink (amir130@hotmail.de)
* Copyright (c) 2018-2021 Amir Czwink (amir130@hotmail.de)
*
* This file is part of ACScript.
*
Expand Down Expand Up @@ -30,4 +30,5 @@ enum class Opcode : uint8
PopAssign,
Push,
Return,
Select,
};
4 changes: 3 additions & 1 deletion parser/lexer.l
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020 Amir Czwink (amir130@hotmail.de)
* Copyright (c) 2020-2021 Amir Czwink (amir130@hotmail.de)
*
* This file is part of ACScript.
*
Expand Down Expand Up @@ -32,6 +32,7 @@ extern AST::ParserState* g_parserState;

"," { return TOKEN_COMMA; }
":" { return TOKEN_COLON; }
"." { return TOKEN_DOT; }
";" { return TOKEN_SEMICOLON; }
":=" { return TOKEN_ASSIGN; }
"->" { return TOKEN_MAP; }
Expand All @@ -44,6 +45,7 @@ extern AST::ParserState* g_parserState;

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

"extern" { return TOKEN_KEYWORD_EXTERN; }
Expand Down
19 changes: 13 additions & 6 deletions parser/parser.y
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020 Amir Czwink (amir130@hotmail.de)
* Copyright (c) 2020-2021 Amir Czwink (amir130@hotmail.de)
*
* This file is part of ACScript.
*
Expand Down Expand Up @@ -29,9 +29,11 @@ void yyerror(AST::ParserState* parserState, const char* s);
%token TOKEN_BRACE_CLOSE
%token TOKEN_COLON
%token TOKEN_COMMA
%token TOKEN_DOT
%token TOKEN_GUARD
%token TOKEN_MAP
%token TOKEN_MORE
%token TOKEN_MULTIPLY
%token TOKEN_PAREN_OPEN
%token TOKEN_PAREN_CLOSE
%token TOKEN_SEMICOLON
Expand Down Expand Up @@ -93,7 +95,9 @@ void yyerror(AST::ParserState* parserState, const char* s);

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

%right infixprec
%left TOKEN_PLUS
%left TOKEN_MULTIPLY

%left TOKEN_IDENTIFIER
%left TOKEN_MAP

Expand All @@ -114,6 +118,7 @@ function_name:
TOKEN_IDENTIFIER { $$ = $1; }
| TOKEN_PLUS { $$ = parserState->CreateString(u8"+"); }
| TOKEN_MINUS { $$ = parserState->CreateString(u8"-"); }
| TOKEN_MULTIPLY { $$ = parserState->CreateString(u8"*"); }
| TOKEN_EQUALS { $$ = parserState->CreateString(u8"="); }
;

Expand All @@ -130,13 +135,15 @@ expression:
;

expression_without_function_value:
TOKEN_IDENTIFIER TOKEN_PAREN_OPEN expression TOKEN_PAREN_CLOSE { $$ = new AST::CallExpression(*$1, $3); }
expression_without_function_value TOKEN_PAREN_OPEN expression TOKEN_PAREN_CLOSE { $$ = new AST::CallExpression($1, $3); }
| expression_without_function_value TOKEN_DOT TOKEN_IDENTIFIER { $$ = new AST::SelectExpression($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))); }
| expression_without_function_value TOKEN_IDENTIFIER expression_without_function_value { $$ = new AST::CallExpression(new AST::IdentifierExpression(*$2), new AST::TupleExpression(new AST::ExpressionList($1, $3))); }
| expression_without_function_value TOKEN_MULTIPLY expression_without_function_value { $$ = new AST::CallExpression(new AST::IdentifierExpression(u8"*"), new AST::TupleExpression(new AST::ExpressionList($1, $3))); }
| expression_without_function_value TOKEN_PLUS expression_without_function_value { $$ = new AST::CallExpression(new AST::IdentifierExpression(u8"+"), new AST::TupleExpression(new AST::ExpressionList($1, $3))); }
| expression_without_function_value TOKEN_EQUALS expression_without_function_value { $$ = new AST::CallExpression(new AST::IdentifierExpression(u8"="), new AST::TupleExpression(new AST::ExpressionList($1, $3))); }
;


Expand Down
3 changes: 2 additions & 1 deletion src_compiler/AST/AllNodes.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020 Amir Czwink (amir130@hotmail.de)
* Copyright (c) 2020-2021 Amir Czwink (amir130@hotmail.de)
*
* This file is part of ACScript.
*
Expand All @@ -23,6 +23,7 @@
#include "expressions/IdentifierExpression.hpp"
#include "expressions/NaturalLiteralExpression.hpp"
#include "expressions/ObjectExpression.hpp"
#include "expressions/SelectExpression.hpp"
#include "expressions/TupleExpression.hpp"

#include "leftValues/IdentifierLeftValue.hpp"
Expand Down
1 change: 1 addition & 0 deletions src_compiler/AST/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ set(SOURCE_FILES_COMPILER
${CMAKE_CURRENT_SOURCE_DIR}/expressions/IdentifierExpression.hpp
${CMAKE_CURRENT_SOURCE_DIR}/expressions/NaturalLiteralExpression.hpp
${CMAKE_CURRENT_SOURCE_DIR}/expressions/ObjectExpression.hpp
${CMAKE_CURRENT_SOURCE_DIR}/expressions/SelectExpression.hpp
${CMAKE_CURRENT_SOURCE_DIR}/expressions/TupleExpression.hpp

${CMAKE_CURRENT_SOURCE_DIR}/leftValues/LeftValue.hpp
Expand Down
19 changes: 12 additions & 7 deletions src_compiler/AST/expressions/CallExpression.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2018-2020 Amir Czwink (amir130@hotmail.de)
* Copyright (c) 2018-2021 Amir Czwink (amir130@hotmail.de)
*
* This file is part of ACScript.
*
Expand All @@ -26,20 +26,25 @@ namespace AST
{
public:
//Constructor
inline CallExpression(const String &functionName, UniquePointer<Expression> &&argument)
: functionName(functionName), arg(Move(argument))
inline CallExpression(UniquePointer<Expression>&& called)
: called(Move(called))
{
}

inline CallExpression(UniquePointer<Expression>&& called, UniquePointer<Expression> &&argument)
: called(Move(called)), arg(Move(argument))
{
}

//Properties
inline const Expression &Argument() const
inline const Expression& Argument() const
{
return *this->arg;
}

inline const String &FunctionName() const
inline const Expression& Called() const
{
return this->functionName;
return *this->called;
}

//Methods
Expand All @@ -50,7 +55,7 @@ namespace AST

private:
//Members
String functionName;
UniquePointer<Expression> called;
UniquePointer<Expression> arg;
};
}
55 changes: 55 additions & 0 deletions src_compiler/AST/expressions/SelectExpression.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright (c) 2021 Amir Czwink (amir130@hotmail.de)
*
* This file is part of ACScript.
*
* ACScript is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* ACScript is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with ACScript. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
//Local
#include "Expression.hpp"

namespace AST
{
class SelectExpression : public Expression
{
public:
//Constructor
inline SelectExpression(UniquePointer<class Expression>&& expr, const String& memberName)
: expr(Move(expr)), memberName(memberName)
{
}

//Properties
inline const Expression& Expression() const
{
return *this->expr;
}

inline const String& MemberName() const
{
return this->memberName;
}

//Methods
void Visit(ExpressionVisitor &visitor) const override
{
visitor.OnVisitingSelectExpression(*this);
}

private:
UniquePointer<class Expression> expr;
String memberName;
};
}
4 changes: 3 additions & 1 deletion src_compiler/AST/visitors/ExpressionVisitor.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020 Amir Czwink (amir130@hotmail.de)
* Copyright (c) 2020-2021 Amir Czwink (amir130@hotmail.de)
*
* This file is part of ACScript.
*
Expand All @@ -26,6 +26,7 @@ namespace AST
class IdentifierExpression;
class NaturalLiteralExpression;
class ObjectExpression;
class SelectExpression;
class TupleExpression;

class ExpressionVisitor
Expand All @@ -37,6 +38,7 @@ namespace AST
virtual void OnVisitingIdentifier(const IdentifierExpression& identifierExpression) = 0;
virtual void OnVisitingNaturalLiteral(const NaturalLiteralExpression& naturalLiteralExpression) = 0;
virtual void OnVisitingObjectExpression(const ObjectExpression& objectExpression) = 0;
virtual void OnVisitingSelectExpression(const SelectExpression& selectExpression) = 0;
virtual void OnVisitedTupleExpression(const TupleExpression& tupleExpression) = 0;
};
}
3 changes: 2 additions & 1 deletion src_compiler/IR/BasicBlock.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
//Local
#include "Instruction.hpp"
#include "visitors/BasicBlockVisitor.hpp"
#include "Scope.hpp"

namespace IR
{
Expand All @@ -32,7 +33,7 @@ namespace IR
}

//Members
Map<String, IR::Value*> namedValues;
Scope scope;

//Properties
inline const DynamicArray<Instruction*>& Instructions() const
Expand Down
Loading

0 comments on commit 01a2789

Please sign in to comment.