grammar done

This commit is contained in:
IlyaShurupov 2024-03-05 20:29:56 +03:00 committed by Ilusha
parent ad543c304d
commit e628e7c1e2
6 changed files with 1612 additions and 1191 deletions

View file

@ -133,7 +133,7 @@ void FunctionDefinition::EvalStatement(Statement* stm) {
auto function_obj = NDO_CAST(MethodObject, NDO->create("method"));
auto method_const_obj = mConstants.addMethod(func.mFunctionId, function_obj);
for (auto child_stm : stm_func_def->mStatements) {
for (auto child_stm : stm_func_def->mStatements->mStatements) {
func.EvalStatement(child_stm.data());
}

View file

@ -3,10 +3,8 @@
using namespace obj;
using namespace BCgen;
StatementFuncDef::StatementFuncDef(tp::String function_id, tp::InitialierList<tp::String> args, tp::InitialierList<Statement*> statements) {
StatementFuncDef::StatementFuncDef(tp::String function_id) {
mType = Type::DEF_FUNC;
mArgs = args;
mStatements = statements;
mFunctionId = function_id;
}
@ -72,7 +70,14 @@ StatementClassDef::StatementClassDef(tp::String class_id, StatementScope* scope)
}
// helpers
StatementFuncDef* obj::BCgen::StmDefFunc(tp::String id, tp::InitialierList<tp::String> args, tp::InitialierList<Statement*> stms) { return new StatementFuncDef(id, args, stms); }
StatementFuncDef*
obj::BCgen::StmDefFunc(tp::String id, tp::InitialierList<tp::String> args, tp::InitialierList<Statement*> stms) {
auto out = new StatementFuncDef(id);
auto scope = new StatementScope(stms, true);
out->mStatements = scope;
out->mArgs = args;
return out;
}
StatementLocalDef* obj::BCgen::StmDefLocal(tp::String id, Expression* value) {
if (value->mType == Expression::Type::CONST_EXPR) {
@ -90,12 +95,20 @@ StatementReturn* obj::BCgen::StmReturn() { return new StatementReturn(); }
StatementReturn* obj::BCgen::StmReturn(Expression* obj) { return new StatementReturn(obj); }
StatementIf* obj::BCgen::StmIf(Expression* condition, StatementScope* on_true, StatementScope* on_false) { return new StatementIf(condition, on_true, on_false); }
StatementIf* obj::BCgen::StmIf(Expression* condition, StatementScope* on_true, StatementScope* on_false) {
return new StatementIf(condition, on_true, on_false);
}
StatementScope* obj::BCgen::StmScope(tp::InitialierList<Statement*> statements, bool aPushToScopeStack = false) { return new StatementScope(statements, aPushToScopeStack); }
StatementScope* obj::BCgen::StmScope(tp::InitialierList<Statement*> statements, bool aPushToScopeStack = false) {
return new StatementScope(statements, aPushToScopeStack);
}
StatementWhile* obj::BCgen::StmWhile(Expression* condition, StatementScope* scope) { return new StatementWhile(condition, scope); }
StatementWhile* obj::BCgen::StmWhile(Expression* condition, StatementScope* scope) {
return new StatementWhile(condition, scope);
}
StatementIgnore* obj::BCgen::StmIgnore(Expression* expr) { return new StatementIgnore(expr); }
StatementClassDef* obj::BCgen::StmClassDef(tp::String id, StatementScope* scope) { return new StatementClassDef(id, scope); }
StatementClassDef* obj::BCgen::StmClassDef(tp::String id, StatementScope* scope) {
return new StatementClassDef(id, scope);
}

View file

@ -7,43 +7,95 @@ using namespace obj::BCgen;
typedef lalr::ParserNode<SymbolType> Node;
static UserData scope(const UserData* start, const Node* nodes, size_t length) { return start[1]; }
static UserData scope(const UserData* start, const Node*, size_t) { return start[1]; }
static UserData stm_list_append(const UserData* start, const Node* nodes, size_t length) {
static UserData scope_empty(const UserData*, const Node*, size_t) { return new StatementScope({}, true); }
static UserData stm_list_append(const UserData* start, const Node*, size_t) {
auto scope = (StatementScope*) start[0];
auto stm = (Statement*) start[2];
scope->mStatements.append(stm);
return scope;
}
static UserData stm_list_create(const UserData* start, const Node* nodes, size_t length) {
static UserData stm_list_create(const UserData* start, const Node*, size_t) {
return new StatementScope({ (Statement*) start[0] }, true);
}
static UserData stm_log(const UserData* start, const Node* nodes, size_t length) {
static UserData stm_log(const UserData* start, const Node*, size_t) {
auto expr = (Expression*) (start[1]);
return new StatementPrint(expr);
}
static UserData stm_assign(const UserData* start, const Node* nodes, size_t length) {
static UserData stm_assign(const UserData* start, const Node*, size_t) {
return new StatementCopy((Expression*) start[0], (Expression*) start[2]);
}
static UserData stm_var_def_new_type(const UserData* start, const Node* nodes, size_t length) {
static UserData stm_var_def_new_type(const UserData*, const Node* nodes, size_t) {
return new StatementLocalDef((char*) nodes[1].lexeme().c_str(), new ExpressionNew((char*) nodes[3].lexeme().c_str()));
}
static UserData stm_var_def_assign(const UserData* start, const Node* nodes, size_t length) {
static UserData stm_var_def_assign(const UserData* start, const Node* nodes, size_t) {
return new StatementLocalDef((char*) nodes[1].lexeme().c_str(), (Expression*) start[3]);
}
static UserData expr_function(const UserData* start, const Node* nodes, size_t length) {
static UserData stm_return(const UserData* start, const Node*, size_t length) {
if (length == 2) {
return new StatementReturn((Expression*) start[1]);
}
return new StatementReturn();
}
static UserData stm_ignore(const UserData* start, const Node*, size_t) {
return new StatementIgnore((Expression*) start[0]);
}
static UserData stm_def_class(const UserData* start, const Node* nodes, size_t) {
auto newClass = new StatementClassDef((char*) nodes[1].lexeme().c_str(), (StatementScope*) start[2]);
return newClass;
}
static UserData stm_def_method(const UserData* start, const Node* nodes, size_t) {
auto method = new StatementFuncDef((char*) nodes[1].lexeme().c_str());
method->mStatements = (StatementScope*) start[5];
auto args = (tp::Buffer<tp::String>*) start[3];
method->mArgs = *args;
delete args;
return method;
}
static UserData stm_if(const UserData* start, const Node*, size_t length) {
if (length == 5) return new StatementIf((Expression*) start[2], (StatementScope*) start[4], nullptr);
return new StatementIf((Expression*) start[2], (StatementScope*) start[4], (StatementScope*) start[6]);
}
static UserData stm_while_loop(const UserData* start, const Node*, size_t) {
return new StatementWhile((Expression*) start[2], (StatementScope*) start[4]);
}
static UserData stm_break(const UserData*, const Node*, size_t) {
ASSERT(0);
// todo : implement break
return nullptr;
}
static UserData id_list_append(const UserData* start, const Node* nodes, size_t) {
auto list = (tp::Buffer<tp::String>*) start[0];
list->append((char*) nodes[1].lexeme().c_str());
return list;
}
static UserData id_list_create(const UserData*, const Node* nodes, size_t) {
return new tp::Buffer<tp::String>({ (char*) nodes[0].lexeme().c_str() });
}
static UserData expr_function(const UserData* start, const Node*, size_t) {
auto expr = (Expression*) (start[0]);
auto args = (ExpressionList*) (start[2]);
return expr->ExprCall(args);
}
static UserData expr_method(const UserData* start, const Node* nodes, size_t length) {
static UserData expr_method(const UserData* start, const Node* nodes, size_t) {
auto expr = (Expression*) (start[0]);
auto args = (ExpressionList*) (start[4]);
auto childId = nodes[1].lexeme();
@ -51,99 +103,99 @@ static UserData expr_method(const UserData* start, const Node* nodes, size_t len
return method->ExprCall(args);
}
static UserData expr_list_append(const UserData* start, const Node* nodes, size_t length) {
static UserData expr_list_append(const UserData* start, const Node*, size_t) {
auto list = (ExpressionList*) start[0];
auto expr = (Expression*) start[2];
list->mItems.append(expr);
return list;
}
static UserData expr_list_create(const UserData* start, const Node* nodes, size_t length) {
static UserData expr_list_create(const UserData* start, const Node*, size_t) {
auto exprList = new ExpressionList();
exprList->mItems.append((Expression*) start[0]);
return exprList;
}
static UserData expr_child(const UserData* start, const Node* nodes, size_t length) {
static UserData expr_child(const UserData* start, const Node* nodes, size_t) {
return new ExpressionChild((Expression*) start[0], (char*) nodes[2].lexeme().c_str());
}
static UserData expr_bool_eq(const UserData* start, const Node* nodes, size_t length) {
static UserData expr_bool_eq(const UserData* start, const Node*, size_t) {
return new ExpressionBoolean((Expression*) start[0], (Expression*) start[2], ExpressionBoolean::BoolType::EQUAL);
}
static UserData expr_bool_negate(const UserData* start, const Node* nodes, size_t length) {
static UserData expr_bool_negate(const UserData* start, const Node*, size_t) {
return new ExpressionBoolean((Expression*) start[1]);
}
static UserData expr_bool_neq(const UserData* start, const Node* nodes, size_t length) {
static UserData expr_bool_neq(const UserData* start, const Node*, size_t) {
return new ExpressionBoolean((Expression*) start[0], (Expression*) start[2], ExpressionBoolean::BoolType::NOT_EQUAL);
}
static UserData expr_bool_grater(const UserData* start, const Node* nodes, size_t length) {
static UserData expr_bool_grater(const UserData* start, const Node*, size_t) {
return new ExpressionBoolean((Expression*) start[0], (Expression*) start[2], ExpressionBoolean::BoolType::MORE);
}
static UserData expr_bool_lesser(const UserData* start, const Node* nodes, size_t length) {
static UserData expr_bool_lesser(const UserData* start, const Node*, size_t) {
return new ExpressionBoolean((Expression*) start[0], (Expression*) start[2], ExpressionBoolean::BoolType::LESS);
}
static UserData expr_bool_grater_eq(const UserData* start, const Node* nodes, size_t length) {
static UserData expr_bool_grater_eq(const UserData* start, const Node*, size_t) {
return new ExpressionBoolean(
(Expression*) start[0], (Expression*) start[2], ExpressionBoolean::BoolType::EQUAL_OR_MORE
);
}
static UserData expr_bool_lesser_eq(const UserData* start, const Node* nodes, size_t length) {
static UserData expr_bool_lesser_eq(const UserData* start, const Node*, size_t) {
return new ExpressionBoolean(
(Expression*) start[0], (Expression*) start[2], ExpressionBoolean::BoolType::EQUAL_OR_LESS
);
}
static UserData expr_add(const UserData* start, const Node* nodes, size_t length) {
static UserData expr_add(const UserData* start, const Node*, size_t) {
return new ExpressionAriphm((Expression*) start[0], (Expression*) start[2], obj::OpCode::OBJ_ADD);
}
static UserData expr_subtract(const UserData* start, const Node* nodes, size_t length) {
static UserData expr_subtract(const UserData* start, const Node*, size_t) {
return new ExpressionAriphm((Expression*) start[0], (Expression*) start[2], obj::OpCode::OBJ_SUB);
}
static UserData expr_multiply(const UserData* start, const Node* nodes, size_t length) {
static UserData expr_multiply(const UserData* start, const Node*, size_t) {
return new ExpressionAriphm((Expression*) start[0], (Expression*) start[2], obj::OpCode::OBJ_MUL);
}
static UserData expr_divide(const UserData* start, const Node* nodes, size_t length) {
static UserData expr_divide(const UserData* start, const Node*, size_t) {
return new ExpressionAriphm((Expression*) start[0], (Expression*) start[2], obj::OpCode::OBJ_DIV);
}
static UserData expr_compound(const UserData* start, const Node* nodes, size_t length) { return start[1]; }
static UserData expr_compound(const UserData* start, const Node*, size_t) { return start[1]; }
static UserData expr_bool(const UserData* start, const Node* nodes, size_t length) {
static UserData expr_bool(const UserData*, const Node* nodes, size_t) {
return new ExpressionConst(nodes[0].lexeme() == "true");
}
static UserData expr_int(const UserData* start, const Node* nodes, size_t length) {
static UserData expr_int(const UserData*, const Node* nodes, size_t) {
return new ExpressionConst(stoi(nodes[0].lexeme()));
}
static UserData expr_float(const UserData* start, const Node* nodes, size_t length) {
static UserData expr_float(const UserData*, const Node* nodes, size_t) {
return new ExpressionConst(stof(nodes[0].lexeme()));
}
static UserData expr_string(const UserData* start, const Node* nodes, size_t length) {
static UserData expr_string(const UserData*, const Node* nodes, size_t) {
return new ExpressionConst((char*) nodes[0].lexeme().c_str());
}
static UserData expr_id(const UserData* start, const Node* nodes, size_t length) {
static UserData expr_id(const UserData*, const Node* nodes, size_t) {
return new ExpressionLocal((char*) nodes[0].lexeme().c_str());
}
static UserData tmp(const UserData* start, const Node* nodes, size_t length) {
// a
static UserData tmp(const UserData* start, const Node*, size_t) {
// pass through
return *start;
}
#define BIND(name) parser.set_action_handler(#name, &(name));
#define BIND(name) parser.set_action_handler(#name, &(name))
void bind(LalrParser& parser) {
BIND(scope);
@ -158,12 +210,40 @@ void bind(LalrParser& parser) {
BIND(stm_var_def_assign);
BIND(stm_log);
BIND(stm_assign);
stm_while_loop stm_return stm_break BIND(expr_function) BIND(expr_method) BIND(expr_child) BIND(expr_bool_eq)
BIND(expr_bool_negate) BIND(expr_bool_neq) BIND(expr_bool_grater) BIND(expr_bool_lesser) BIND(expr_bool_grater_eq)
BIND(expr_bool_lesser_eq) BIND(expr_add) BIND(expr_subtract) BIND(expr_multiply) BIND(expr_divide)
BIND(expr_compound) BIND(expr_bool) BIND(expr_int) BIND(expr_float) BIND(expr_string) BIND(expr_id)
BIND(stm_ignore);
BIND(tmp)
BIND(stm_while_loop);
BIND(stm_return);
BIND(stm_break);
BIND(scope_empty);
BIND(stm_def_method);
BIND(stm_def_class);
BIND(stm_if);
BIND(id_list_append);
BIND(id_list_create);
BIND(expr_function);
BIND(expr_method);
BIND(expr_child);
BIND(expr_bool_eq);
BIND(expr_bool_negate);
BIND(expr_bool_neq);
BIND(expr_bool_grater);
BIND(expr_bool_lesser);
BIND(expr_bool_grater_eq);
BIND(expr_bool_lesser_eq);
BIND(expr_add);
BIND(expr_subtract);
BIND(expr_multiply);
BIND(expr_divide);
BIND(expr_compound);
BIND(expr_bool);
BIND(expr_int);
BIND(expr_float);
BIND(expr_string);
BIND(expr_id);
BIND(tmp);
parser.set_lexer_action_handler("string", &lalr::string_literal<IteratorType>);
}

File diff suppressed because it is too large Load diff

View file

@ -13,27 +13,27 @@ oscript {
scope:
'{' stmts '}' [scope]
| ;
| '{' '}' [scope_empty];
stmts:
stmts ';' stmt ';' [stm_list_append]
| stmt [stm_list_create]
| ;
stmts stmt [stm_list_append]
| stmt [stm_list_create];
stmt:
stm_var_def_new_type [tmp]
| stm_var_def_assign [tmp]
| stm_log [tmp]
| stm_assign [tmp]
stm_var_def_new_type ';' [tmp]
| stm_var_def_assign ';' [tmp]
| stm_log ';' [tmp]
| stm_assign ';' [tmp]
| stm_while [tmp]
| stm_return [tmp]
| stm_break [tmp]
| stm_return ';' [tmp]
| stm_break ';' [tmp]
| stm_if [tmp]
| stm_def_class [tmp]
| stm_def_method [tmp]
| stm_ignore ';' [tmp]
;
stm_def_method: 'class' id scope [stm_def_class];
stm_def_method: 'method' id '(' id_list ')' scope [stm_def_method];
stm_def_class: 'class' id scope [stm_def_class];
stm_if:
@ -46,13 +46,19 @@ oscript {
stm_var_def_new_type: 'var' id ':' id [stm_var_def_new_type];
stm_var_def_assign: 'var' id '=' expr [stm_var_def_assign];
stm_log: 'print' expr [stm_log];
stm_assign: expr '=' expr [stm_assign];
stm_assign : expr '=' expr [stm_assign];
stm_ignore : expr [stm_ignore];
expr_list:
expr_list ',' expr ',' [expr_list_append]
| expr [expr_list_create]
| ;
id_list:
id_list ',' id ',' [id_list_append]
| id [id_list_create]
| ;
expr:
id '(' expr_list ')' [expr_function]
| expr '.' id '(' expr_list ')' [expr_method]

View file

@ -37,9 +37,9 @@ namespace obj {
struct StatementFuncDef : public Statement {
tp::Buffer<tp::String> mArgs;
tp::String mFunctionId;
tp::Buffer<Statement*> mStatements;
StatementScope* mStatements;
StatementFuncDef(tp::String function_id, tp::InitialierList<tp::String> args, tp::InitialierList<Statement*> statements);
StatementFuncDef(tp::String function_id);
};
struct StatementLocalDef : public Statement {
@ -101,7 +101,8 @@ namespace obj {
};
// Helpers
StatementFuncDef* StmDefFunc(tp::String id, tp::InitialierList<tp::String> args, tp::InitialierList<Statement*> stms);
StatementFuncDef*
StmDefFunc(tp::String id, tp::InitialierList<tp::String> args, tp::InitialierList<Statement*> stms);
StatementLocalDef* StmDefLocal(tp::String id, Expression* value);
StatementCopy* StmCopy(Expression* left, Expression* right);
StatementPrint* StmPrint(Expression* target);