From e755850c6fecb2251d62fcc1c03e5d09ad61559e Mon Sep 17 00:00:00 2001 From: TopchetoEU <36534413+TopchetoEU@users.noreply.github.com> Date: Fri, 28 Oct 2022 10:14:58 +0300 Subject: [PATCH] feat: add string parser --- src/compiler/treeifier/ast/ast.cc | 4 ++-- src/compiler/treeifier/ast/parsers/exp.cc | 13 ++++++++++++- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/src/compiler/treeifier/ast/ast.cc b/src/compiler/treeifier/ast/ast.cc index 13ba48b..912ed86 100644 --- a/src/compiler/treeifier/ast/ast.cc +++ b/src/compiler/treeifier/ast/ast.cc @@ -13,9 +13,9 @@ group_t &ast_ctx_t::group(const std::string &name) { ast_ctx_t::ast_ctx_t(msg_stack_t &messages, std::vector &tokens): messages(messages), tokens(tokens) { group("$_exp_val") .add_last("$_var", parse_exp_var) - .add_last("$_int", parse_exp_int_lit); + .add_last("$_int", parse_exp_int_lit) + .add_last("$_string", parse_exp_str_lit); // .add_last("$_float", parse_exp_float_lit) - // .add_last("$_string", parse_exp_str_lit); group("$_stat") .add_last("$_exp", parse_stat_exp); group("$_def") diff --git a/src/compiler/treeifier/ast/parsers/exp.cc b/src/compiler/treeifier/ast/parsers/exp.cc index 690eb50..7991e2e 100644 --- a/src/compiler/treeifier/ast/parsers/exp.cc +++ b/src/compiler/treeifier/ast/parsers/exp.cc @@ -159,7 +159,7 @@ bool ast::parse_exp_var(ast_ctx_t &ctx, size_t &res_i, map_t &out) { bool ast::parse_exp_int_lit(ast_ctx_t &ctx, size_t &res_i, map_t &out) { tree_helper_t h(ctx, res_i); - if (h.curr().is_literal()) { + if (h.curr().is_int_literal()) { auto &arr = out["content"].array({}); for (auto b : h.curr().literal()) arr.push_back((float)b); out["location"] = conv::loc_to_map(h.loc()); @@ -168,7 +168,18 @@ bool ast::parse_exp_int_lit(ast_ctx_t &ctx, size_t &res_i, map_t &out) { return false; } +bool ast::parse_exp_str_lit(ast_ctx_t &ctx, size_t &res_i, map_t &out) { + tree_helper_t h(ctx, res_i); + if (h.curr().is_str_literal()) { + auto &arr = out["content"].array({}); + for (auto b : h.curr().literal()) arr.push_back((float)b); + out["location"] = conv::loc_to_map(h.loc()); + return h.submit(true); + } + + return false; +} bool ast::parse_exp(ast_ctx_t &ctx, size_t &res_i, map_t &out) { tree_helper_t h(ctx, res_i);