diff --git a/include/cubescript/cubescript.hh b/include/cubescript/cubescript.hh index b20e2c5..aa69661 100644 --- a/include/cubescript/cubescript.hh +++ b/include/cubescript/cubescript.hh @@ -211,7 +211,6 @@ struct LIBCUBESCRIPT_EXPORT ident { int get_raw_type() const; ident_type get_type() const; std::string_view get_name() const; - int get_flags() const; int get_index() const; bool is_alias() const; @@ -243,13 +242,14 @@ struct LIBCUBESCRIPT_EXPORT ident { protected: ident() = default; -private: friend struct state; ident_impl *p_impl{}; }; struct LIBCUBESCRIPT_EXPORT global_var: ident { + bool is_read_only() const; + protected: global_var() = default; }; @@ -279,6 +279,8 @@ protected: }; struct LIBCUBESCRIPT_EXPORT alias: ident { + bool is_arg() const; + protected: alias() = default; }; diff --git a/src/cs_ident.cc b/src/cs_ident.cc index defc89f..abd45e5 100644 --- a/src/cs_ident.cc +++ b/src/cs_ident.cc @@ -142,10 +142,6 @@ LIBCUBESCRIPT_EXPORT std::string_view ident::get_name() const { return p_impl->p_name; } -LIBCUBESCRIPT_EXPORT int ident::get_flags() const { - return p_impl->p_flags; -} - LIBCUBESCRIPT_EXPORT int ident::get_index() const { return p_impl->p_index; } @@ -270,6 +266,10 @@ LIBCUBESCRIPT_EXPORT string_var const *ident::get_svar() const { return static_cast(this); } +LIBCUBESCRIPT_EXPORT bool global_var::is_read_only() const { + return (p_impl->p_flags & IDENT_FLAG_READONLY); +} + LIBCUBESCRIPT_EXPORT integer_type integer_var::get_value() const { return static_cast(this)->p_storage; } @@ -294,6 +294,10 @@ LIBCUBESCRIPT_EXPORT void string_var::set_value(string_ref val) { static_cast(this)->p_storage = val; } +LIBCUBESCRIPT_EXPORT bool alias::is_arg() const { + return (static_cast(this)->p_flags & IDENT_FLAG_ARG); +} + LIBCUBESCRIPT_EXPORT std::string_view command::get_args() const { return static_cast(this)->p_cargs; } diff --git a/src/cs_state.cc b/src/cs_state.cc index a80cb6e..55682c1 100644 --- a/src/cs_state.cc +++ b/src/cs_state.cc @@ -330,7 +330,7 @@ LIBCUBESCRIPT_EXPORT void state::set_alias( case ident_type::ALIAS: { alias *a = static_cast(id); auto &ast = p_tstate->get_astack(a); - if (a->get_flags() & IDENT_FLAG_ARG) { + if (a->is_arg()) { ast.set_arg(a, *p_tstate, v); } else { ast.set_alias(a, *p_tstate, v); @@ -421,7 +421,7 @@ state::get_alias_val(std::string_view name) { if (!a) { return std::nullopt; } - if ((a->get_flags() & IDENT_FLAG_ARG) && !ident_is_used_arg(a, *p_tstate)) { + if (a->is_arg() && !ident_is_used_arg(a, *p_tstate)) { return std::nullopt; } return p_tstate->get_astack(a).node->val_s.get_str(); @@ -566,10 +566,7 @@ LIBCUBESCRIPT_EXPORT void state::run( } case ident_type::ALIAS: { alias *a = static_cast(id); - if ( - (a->get_flags() & IDENT_FLAG_ARG) && - !ident_is_used_arg(a, *p_tstate) - ) { + if (a->is_arg() && !ident_is_used_arg(a, *p_tstate)) { break; } exec_alias( diff --git a/src/cs_vm.cc b/src/cs_vm.cc index c6a737e..9c4e84a 100644 --- a/src/cs_vm.cc +++ b/src/cs_vm.cc @@ -10,7 +10,7 @@ namespace cubescript { static inline void push_alias(thread_state &ts, ident *id, ident_stack &st) { - if (id->is_alias() && !(id->get_flags() & IDENT_FLAG_ARG)) { + if (id->is_alias() && !static_cast(id)->is_arg()) { auto *aimp = static_cast(id); ts.get_astack(aimp).push(st); aimp->p_flags &= ~IDENT_FLAG_UNKNOWN; @@ -18,7 +18,7 @@ static inline void push_alias(thread_state &ts, ident *id, ident_stack &st) { } static inline void pop_alias(thread_state &ts, ident *id) { - if (id->is_alias() && !(id->get_flags() & IDENT_FLAG_ARG)) { + if (id->is_alias() && !static_cast(id)->is_arg()) { ts.get_astack(static_cast(id)).pop(); } } @@ -285,13 +285,13 @@ run_depth_guard::~run_depth_guard() { --rundepth; } static inline alias *get_lookup_id(thread_state &ts, std::uint32_t op) { ident *id = ts.istate->identmap[op >> 8]; + auto *a = static_cast(id); - auto flags = id->get_flags(); - if (flags & IDENT_FLAG_ARG) { + if (a->is_arg()) { if (!ident_is_used_arg(id, ts)) { return nullptr; } - } else if (flags & IDENT_FLAG_UNKNOWN) { + } else if (a->p_flags & IDENT_FLAG_UNKNOWN) { throw error{ *ts.pstate, "unknown alias lookup: %s", id->get_name().data() }; @@ -308,17 +308,16 @@ static inline int get_lookupu_type( id = ts.pstate->get_ident(arg.get_str()); if (id) { switch(id->get_type()) { - case ident_type::ALIAS: - if (id->get_flags() & IDENT_FLAG_UNKNOWN) { + case ident_type::ALIAS: { + auto *a = static_cast(id); + if (a->p_flags & IDENT_FLAG_UNKNOWN) { break; } - if ( - (id->get_flags() & IDENT_FLAG_ARG) && - !ident_is_used_arg(id, ts) - ) { + if (a->is_arg() && !ident_is_used_arg(id, ts)) { return ID_UNKNOWN; } return ID_ALIAS; + } case ident_type::SVAR: return ID_SVAR; case ident_type::IVAR: @@ -745,10 +744,7 @@ std::uint32_t *vm_exec( alias *a = static_cast( ts.istate->identmap[op >> 8] ); - if ( - (a->get_flags() & IDENT_FLAG_ARG) && - !ident_is_used_arg(a, ts) - ) { + if (a->is_arg() && !ident_is_used_arg(a, ts)) { ts.get_astack(a).push(ts.idstack.emplace_back(*ts.pstate)); ts.callstack->usedargs[a->get_index()] = true; } @@ -761,11 +757,8 @@ std::uint32_t *vm_exec( if (arg.get_type() == value_type::STRING) { id = cs.new_ident(arg.get_str(), IDENT_FLAG_UNKNOWN); } - if ( - (id->get_flags() & IDENT_FLAG_ARG) && - !ident_is_used_arg(id, ts) - ) { - auto *a = static_cast(id); + alias *a = static_cast(id); + if (a->is_arg() && !ident_is_used_arg(id, ts)) { ts.get_astack(a).push(ts.idstack.emplace_back(*ts.pstate)); ts.callstack->usedargs[id->get_index()] = true; } @@ -1022,7 +1015,7 @@ std::uint32_t *vm_exec( ts.istate->identmap[op >> 8] ); auto &ast = ts.get_astack(a); - if (a->get_flags() & IDENT_FLAG_ARG) { + if (a->is_arg()) { ast.set_arg(a, ts, args.back()); } else { ast.set_alias(a, ts, args.back()); @@ -1047,21 +1040,21 @@ std::uint32_t *vm_exec( std::size_t callargs = *code++; std::size_t nnargs = args.size(); std::size_t offset = nnargs - callargs; - auto flags = id->get_flags(); - if (flags & IDENT_FLAG_ARG) { + auto *imp = static_cast(id); + if (imp->is_arg()) { if (!ident_is_used_arg(id, ts)) { args.resize(offset, any_value{cs}); force_arg(result, op & BC_INST_RET_MASK); continue; } - } else if (flags & IDENT_FLAG_UNKNOWN) { + } else if (imp->p_flags & IDENT_FLAG_UNKNOWN) { force_arg(result, op & BC_INST_RET_MASK); throw error{ cs, "unknown command: %s", id->get_name().data() }; } exec_alias( - ts, static_cast(id), &args[0], result, callargs, + ts, imp, &args[0], result, callargs, nnargs, offset, 0, op ); args.resize(nnargs, any_value{cs}); @@ -1197,10 +1190,7 @@ noid: } case ID_ALIAS: { alias *a = static_cast(id); - if ( - (a->get_flags() & IDENT_FLAG_ARG) && - !ident_is_used_arg(a, ts) - ) { + if (a->is_arg() && !ident_is_used_arg(a, ts)) { args.resize(offset - 1, any_value{cs}); force_arg(result, op & BC_INST_RET_MASK); continue; diff --git a/src/lib_base.cc b/src/lib_base.cc index 86a7729..c7a005b 100644 --- a/src/lib_base.cc +++ b/src/lib_base.cc @@ -159,7 +159,7 @@ void init_lib_base(state &gcs) { gcs.new_command("pushif", "rte", [](auto &cs, auto args, auto &res) { if (alias_local st{cs, args[0].get_ident()}; st) { - if (st.get_alias()->get_flags() & IDENT_FLAG_ARG) { + if (st.get_alias()->is_arg()) { return; } if (args[1].get_bool()) { @@ -304,7 +304,7 @@ end: gcs.new_command("push", "rte", [](auto &cs, auto args, auto &res) { if (alias_local st{cs, args[0].get_ident()}; st) { - if (st.get_alias()->get_flags() & IDENT_FLAG_ARG) { + if (st.get_alias()->is_arg()) { return; } st.set(args[1]);