More work on binder, implements basic literal expressions
This commit is contained in:
@@ -2,5 +2,48 @@
|
||||
#include "Binder.hpp"
|
||||
|
||||
BoundScriptStatement *Binder::Bind(ParsedScriptStatement *s) {
|
||||
return nullptr;
|
||||
auto binder = Binder();
|
||||
auto statements = s->GetStatements();
|
||||
vector<BoundStatement*> boundStatements (statements.size());
|
||||
for (int i = 0; i < statements.size(); i++){
|
||||
boundStatements[i] = binder.BindStatement(statements[i]);
|
||||
}
|
||||
return new BoundScriptStatement(boundStatements);
|
||||
}
|
||||
|
||||
BoundStatement* Binder::BindStatement(ParsedStatement* statement){
|
||||
switch (statement -> GetKind()) {
|
||||
case ParsedStatementKind ::Script: throw; // This shouldn't happen.
|
||||
case ParsedStatementKind ::Block: return this -> BindBlockStatement(statement);
|
||||
case ParsedStatementKind ::Expression: return this -> BindExpressionStatement(statement);
|
||||
}
|
||||
}
|
||||
|
||||
BoundStatement *Binder::BindBlockStatement(ParsedStatement *statement) {
|
||||
auto statements = ((ParsedBlockStatement*)statement)->GetStatements();
|
||||
vector<BoundStatement*> boundStatements (statements.size());
|
||||
for (int i = 0; i < statements.size(); i++){
|
||||
boundStatements[i] = this -> BindStatement(statements[i]);
|
||||
}
|
||||
return new BoundBlockStatement(boundStatements);
|
||||
}
|
||||
|
||||
BoundStatement *Binder::BindExpressionStatement(ParsedStatement *statement) {
|
||||
auto exp = ((ParsedExpressionStatement*)statement)->GetExpression();
|
||||
return new BoundExpressionStatement(this -> BindExpression(exp));
|
||||
}
|
||||
|
||||
BoundExpression* Binder::BindExpression(ParsedExpression* expression){
|
||||
switch (expression -> GetKind()){
|
||||
case ParsedExpressionKind ::LiteralInteger:
|
||||
return new BoundLiteralIntegerExpression(((LiteralIntegerExpression*)expression)->GetValue(), expression->GetStartPosition(), expression->GetLength());
|
||||
case ParsedExpressionKind ::LiteralFloat:
|
||||
return new BoundLiteralFloatExpression(((LiteralFloatExpression*)expression)->GetValue(), expression->GetStartPosition(), expression->GetLength());
|
||||
case ParsedExpressionKind ::LiteralBool:
|
||||
return new BoundLiteralBoolExpression(((LiteralBoolExpression*)expression)->GetValue(), expression->GetStartPosition(), expression->GetLength());
|
||||
|
||||
case ParsedExpressionKind ::Bad:
|
||||
return new BoundBadExpression(expression->GetStartPosition(), expression-> GetLength());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,13 +1,19 @@
|
||||
|
||||
#ifndef PORYGONLANG_BINDER_HPP
|
||||
#define PORYGONLANG_BINDER_HPP
|
||||
|
||||
#include "../Parser/ParsedStatements/ParsedScriptStatement.hpp"
|
||||
#include "../Parser/ParsedStatements/ParsedStatement.hpp"
|
||||
#include "BoundStatements/BoundStatement.hpp"
|
||||
|
||||
|
||||
class Binder {
|
||||
BoundStatement *BindStatement(ParsedStatement *statement);
|
||||
BoundStatement *BindBlockStatement(ParsedStatement *statement);
|
||||
public:
|
||||
static BoundScriptStatement* Bind(ParsedScriptStatement* s);
|
||||
|
||||
BoundExpression *BindExpression(ParsedExpression *expression);
|
||||
|
||||
BoundStatement *BindExpressionStatement(ParsedStatement *statement);
|
||||
};
|
||||
|
||||
|
||||
|
||||
133
src/Binder/BoundExpressions/BoundExpression.hpp
Normal file
133
src/Binder/BoundExpressions/BoundExpression.hpp
Normal file
@@ -0,0 +1,133 @@
|
||||
#include <utility>
|
||||
|
||||
|
||||
#ifndef PORYGONLANG_BOUNDEXPRESSION_HPP
|
||||
#define PORYGONLANG_BOUNDEXPRESSION_HPP
|
||||
|
||||
#include <string>
|
||||
#include "../../ScriptType.hpp"
|
||||
|
||||
using namespace std;
|
||||
|
||||
enum class BoundExpressionKind{
|
||||
Bad,
|
||||
|
||||
LiteralInteger,
|
||||
LiteralFloat,
|
||||
LiteralString,
|
||||
LiteralBool,
|
||||
|
||||
Unary,
|
||||
Binary,
|
||||
Parenthesized,
|
||||
};
|
||||
|
||||
class BoundExpression{
|
||||
unsigned int _start;
|
||||
unsigned int _length;
|
||||
ScriptType* _type;
|
||||
public:
|
||||
BoundExpression(unsigned int start, unsigned int length, ScriptType* type){
|
||||
_start = start;
|
||||
_length = length;
|
||||
_type = type;
|
||||
}
|
||||
virtual ~BoundExpression(){
|
||||
delete _type;
|
||||
};
|
||||
|
||||
virtual BoundExpressionKind GetKind() = 0;
|
||||
virtual ScriptType* GetType(){
|
||||
return _type;
|
||||
};
|
||||
|
||||
unsigned int GetStartPosition(){
|
||||
return _start;
|
||||
}
|
||||
unsigned int GetLength(){
|
||||
return _length;
|
||||
}
|
||||
};
|
||||
|
||||
class BoundBadExpression : public BoundExpression{
|
||||
public:
|
||||
BoundBadExpression(unsigned int start, unsigned int length) : BoundExpression(start, length, new ScriptType(TypeClass::Error)){}
|
||||
|
||||
BoundExpressionKind GetKind() final{
|
||||
return BoundExpressionKind ::Bad;
|
||||
}
|
||||
};
|
||||
|
||||
class BoundLiteralIntegerExpression : public BoundExpression{
|
||||
long _value;
|
||||
public:
|
||||
BoundLiteralIntegerExpression(long value, unsigned int start, unsigned int length)
|
||||
: BoundExpression(start, length, new NumericScriptType(true, false)){
|
||||
_value = value;
|
||||
}
|
||||
|
||||
BoundExpressionKind GetKind() final{
|
||||
return BoundExpressionKind ::LiteralInteger;
|
||||
}
|
||||
|
||||
long GetValue(){
|
||||
return _value;
|
||||
}
|
||||
};
|
||||
|
||||
class BoundLiteralFloatExpression : public BoundExpression{
|
||||
double _value;
|
||||
public:
|
||||
BoundLiteralFloatExpression(double value, unsigned int start, unsigned int length)
|
||||
: BoundExpression(start, length, new NumericScriptType(true, true)){
|
||||
_value = value;
|
||||
}
|
||||
|
||||
BoundExpressionKind GetKind() final{
|
||||
return BoundExpressionKind ::LiteralFloat;
|
||||
}
|
||||
|
||||
double GetValue(){
|
||||
return _value;
|
||||
}
|
||||
};
|
||||
|
||||
class BoundLiteralStringExpression : public BoundExpression{
|
||||
string _value;
|
||||
public:
|
||||
BoundLiteralStringExpression(string value, unsigned int start, unsigned int length)
|
||||
: BoundExpression(start, length, new ScriptType(TypeClass::String)){
|
||||
_value = std::move(value);
|
||||
}
|
||||
|
||||
BoundExpressionKind GetKind() final{
|
||||
return BoundExpressionKind ::LiteralString;
|
||||
}
|
||||
|
||||
string GetValue(){
|
||||
return _value;
|
||||
}
|
||||
};
|
||||
|
||||
class BoundLiteralBoolExpression : public BoundExpression{
|
||||
bool _value;
|
||||
public:
|
||||
BoundLiteralBoolExpression(bool value, unsigned int start, unsigned int length)
|
||||
: BoundExpression(start, length, new ScriptType(TypeClass::Bool)){
|
||||
_value = value;
|
||||
}
|
||||
|
||||
BoundExpressionKind GetKind() final{
|
||||
return BoundExpressionKind ::LiteralBool;
|
||||
}
|
||||
|
||||
bool GetValue(){
|
||||
return _value;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
#endif //PORYGONLANG_BOUNDEXPRESSION_HPP
|
||||
@@ -5,11 +5,14 @@
|
||||
#define PORYGONLANG_BOUNDSTATEMENT_HPP
|
||||
|
||||
#include <vector>
|
||||
#include "../BoundExpressions/BoundExpression.hpp"
|
||||
|
||||
using namespace std;
|
||||
|
||||
enum class BoundStatementKind{
|
||||
Script,
|
||||
Block,
|
||||
Expression,
|
||||
};
|
||||
|
||||
class BoundStatement{
|
||||
@@ -46,4 +49,19 @@ public:
|
||||
}
|
||||
};
|
||||
|
||||
class BoundExpressionStatement : public BoundStatement{
|
||||
BoundExpression* _expression;
|
||||
public:
|
||||
explicit BoundExpressionStatement(BoundExpression* expression){
|
||||
_expression = expression;
|
||||
}
|
||||
~BoundExpressionStatement() final{
|
||||
delete _expression;
|
||||
}
|
||||
|
||||
BoundStatementKind GetKind() final{
|
||||
return BoundStatementKind ::Expression;
|
||||
}
|
||||
};
|
||||
|
||||
#endif //PORYGONLANG_BOUNDSTATEMENT_HPP
|
||||
|
||||
Reference in New Issue
Block a user