diff --git a/src/grammar.peg b/src/grammar.peg index 1ac5ab6..317d427 100644 --- a/src/grammar.peg +++ b/src/grammar.peg @@ -187,7 +187,7 @@ FLOAT STRINGLITERAL <- STRINGLITERALSINGLE / (line_string skip)+ -STRINGLITERALSINGLE <- "\"" string_char* "\"" skip +STRINGLITERALSINGLE <- "\"" < string_char* > "\"" skip ~bin_int <- bin bin_* ~oct_int <- oct oct_* diff --git a/src/main.cpp b/src/main.cpp index ddc4df2..a4d64f0 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,6 +1,7 @@ #include #include #include +#include #include #include "context.h" @@ -56,6 +57,21 @@ int main_old(void) { return 0; } +std::any readIdentifier(Context &cxt, std::any val) { + assert(val.type() == typeid(Identifier)); + return cxt.currentScope->getVar(cxt, any_cast(val).identifier); +} + +/** + * Returns the value of the input (reads the value if it is a refers to a variable) + */ +std::any getValue(Context& cxt, std::any input) { + if (input.type() == typeid(Identifier)) + return readIdentifier(cxt, input); + else + return input; +} + std::any eval(Context &cxt, peg::Ast& ast) { const auto &nodes = ast.nodes; if (ast.name == "Root") { @@ -79,6 +95,41 @@ std::any eval(Context &cxt, peg::Ast& ast) { cxt.builtins.at(identifier.identifier).execute(cxt, args); // TODO: handle returns return Nil(); + } else if (ast.name == "STRINGLITERALSINGLE") + { + return ast.token_to_string(); + } else if (ast.name == "ExprList") { + std::vector exprs; + for (int i=0; i(value) + any_cast(v2); + } else { + value = any_cast(value) - any_cast(v2); + } + } + return value; + } else if (ast.name == "MultiplyExpr") { + auto value = getValue(cxt, eval(cxt, *nodes[0])); + for (int i=1; i(value) * any_cast(v2); + } else { + value = any_cast(value) / any_cast(v2); + } + } + return value; } std::cout << "AST Name: " << ast.name << std::endl; @@ -94,8 +145,9 @@ int main(void) { #include "grammar.peg" ); std::string s = R"( - var a = 10; - @print(a); + var a = "hello world"; + var b = 20 + 10 + 5 - 10 * 2 / 2; + @print(a,b); )"; parser.enable_ast(); diff --git a/src/util.cpp b/src/util.cpp index 040468d..7b11b02 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -10,6 +10,10 @@ Args Args::toArgs(Context& cxt, std::any input) { auto id = std::any_cast(input); auto value = cxt.currentScope->getVar(cxt, id.identifier); args.args.push_back(value); + } else if (input.type() == typeid(std::vector)) { + args.args = std::any_cast>(input); + } else { + cxt.error("Unimplemented toArgs"); } return args; } @@ -17,8 +21,10 @@ Args Args::toArgs(Context& cxt, std::any input) { void BuiltinFunctions::print(Context& cxt, Args& args) { for (int i=0; i(var) << '\t'; + } else if (var.type() == typeid(std::string)) { + std::cout << std::any_cast(var) << '\t'; } else { cxt.error("Attempt to print unsupported type"); }