From 0c2de8979105e6b5a24be18d3241a609f9bfed8f Mon Sep 17 00:00:00 2001 From: Ben Longbons Date: Tue, 7 Oct 2014 00:05:57 -0700 Subject: Minimize script header --- src/map/fwd.hpp | 5 +++ src/map/magic-v2.cpp | 2 + src/map/npc.hpp | 2 + src/map/script.cpp | 92 ++++++++++++++++++++++++++++++++++++++++++++++ src/map/script.hpp | 101 +++++++++------------------------------------------ 5 files changed, 118 insertions(+), 84 deletions(-) (limited to 'src') diff --git a/src/map/fwd.hpp b/src/map/fwd.hpp index 79bbbcd..b73d36a 100644 --- a/src/map/fwd.hpp +++ b/src/map/fwd.hpp @@ -20,6 +20,8 @@ #include "../sanity.hpp" +#include + namespace tmwa { @@ -40,6 +42,9 @@ struct NpcEvent; struct item_data; +enum class SP : uint16_t; +struct ScriptLabel; + namespace magic { struct fun_t; diff --git a/src/map/magic-v2.cpp b/src/map/magic-v2.cpp index 5b375b2..b299279 100644 --- a/src/map/magic-v2.cpp +++ b/src/map/magic-v2.cpp @@ -24,6 +24,8 @@ #include #include +#include "../range/slice.hpp" + #include "../strings/rstring.hpp" #include "../strings/literal.hpp" diff --git a/src/map/npc.hpp b/src/map/npc.hpp index 33dd378..3870a39 100644 --- a/src/map/npc.hpp +++ b/src/map/npc.hpp @@ -24,6 +24,8 @@ #include +#include "../range/slice.hpp" + #include "../strings/fwd.hpp" #include "../generic/fwd.hpp" diff --git a/src/map/script.cpp b/src/map/script.cpp index 04fc11a..61233b8 100644 --- a/src/map/script.cpp +++ b/src/map/script.cpp @@ -80,6 +80,35 @@ namespace tmwa constexpr bool DEBUG_DISP = false; constexpr bool DEBUG_RUN = false; +enum class VariableCode : uint8_t +{ + PARAM, + VARIABLE, +}; + +enum class StringCode : uint8_t +{ + NOP, POS, INT, PARAM, FUNC, + VARIABLE, +}; + +enum class ByteCode : uint8_t +{ + // types and specials + // Note that 'INT' is synthetic, and does not occur in the data stream + NOP, POS, INT, PARAM, FUNC, STR, ARG, + VARIABLE, EOL, + + // unary and binary operators + LOR, LAND, LE, LT, GE, GT, EQ, NE, + XOR, OR, AND, ADD, SUB, MUL, DIV, MOD, + NEG, LNOT, NOT, R_SHIFT, L_SHIFT, + + // additions + // needed because FUNC is used for the actual call + FUNC_REF, +}; + struct str_data_t { StringCode type; @@ -88,6 +117,69 @@ struct str_data_t int label_; int val; }; + +class ScriptBuffer +{ + typedef ZString::iterator ZSit; + + std::vector script_buf; +public: + // construction methods used only by script.cpp + void add_scriptc(ByteCode a); + void add_scriptb(uint8_t a); + void add_scripti(uint32_t a); + void add_scriptl(str_data_t *a); + void set_label(str_data_t *ld, int pos_); + ZSit parse_simpleexpr(ZSit p); + ZSit parse_subexpr(ZSit p, int limit); + ZSit parse_expr(ZSit p); + ZSit parse_line(ZSit p, bool *canstep); + void parse_script(ZString src, int line, bool implicit_end); + + // consumption methods used only by script.cpp + ByteCode operator[](size_t i) const { return script_buf[i]; } + ZString get_str(size_t i) const + { + return ZString(strings::really_construct_from_a_pointer, reinterpret_cast(&script_buf[i]), nullptr); + } +}; +} // namespace tmwa + +void std::default_delete::operator()(const tmwa::ScriptBuffer *sd) +{ + really_delete1 sd; +} + +namespace tmwa +{ +ByteCode ScriptPointer::peek() const { return (*code)[pos]; } +ByteCode ScriptPointer::pop() { return (*code)[pos++]; } +ZString ScriptPointer::pops() +{ + ZString rv = code->get_str(pos); + pos += rv.size(); + ++pos; + return rv; +} + +struct script_stack +{ + std::vector stack_datav; +}; + +enum class ScriptEndState; +// future improvements coming! +class ScriptState +{ +public: + struct script_stack *stack; + int start, end; + ScriptEndState state; + BlockId rid, oid; + ScriptPointer scriptp, new_scriptp; + int defsp, new_defsp; +}; + static Map str_datam; static diff --git a/src/map/script.hpp b/src/map/script.hpp index 96e14fc..19dbcd0 100644 --- a/src/map/script.hpp +++ b/src/map/script.hpp @@ -24,9 +24,10 @@ #include +#include #include -#include "../range/slice.hpp" +#include "../range/fwd.hpp" #include "../strings/zstring.hpp" @@ -36,71 +37,27 @@ #include "../mmo/ids.hpp" -#include "clif.t.hpp" -#include "map.t.hpp" - namespace tmwa { -enum class VariableCode : uint8_t -{ - PARAM, - VARIABLE, -}; +enum class ByteCode : uint8_t; -enum class StringCode : uint8_t -{ - NOP, POS, INT, PARAM, FUNC, - VARIABLE, -}; +class ScriptBuffer; +} // namespace tmwa -enum class ByteCode : uint8_t +namespace std { - // types and specials - // Note that 'INT' is synthetic, and does not occur in the data stream - NOP, POS, INT, PARAM, FUNC, STR, ARG, - VARIABLE, EOL, - - // unary and binary operators - LOR, LAND, LE, LT, GE, GT, EQ, NE, - XOR, OR, AND, ADD, SUB, MUL, DIV, MOD, - NEG, LNOT, NOT, R_SHIFT, L_SHIFT, - - // additions - // needed because FUNC is used for the actual call - FUNC_REF, -}; - -struct str_data_t; - -class ScriptBuffer +template<> +struct default_delete { - typedef ZString::iterator ZSit; - - std::vector script_buf; -public: - // construction methods used only by script.cpp - void add_scriptc(ByteCode a); - void add_scriptb(uint8_t a); - void add_scripti(uint32_t a); - void add_scriptl(str_data_t *a); - void set_label(str_data_t *ld, int pos_); - ZSit parse_simpleexpr(ZSit p); - ZSit parse_subexpr(ZSit p, int limit); - ZSit parse_expr(ZSit p); - ZSit parse_line(ZSit p, bool *canstep); - void parse_script(ZString src, int line, bool implicit_end); - - // consumption methods used only by script.cpp - ByteCode operator[](size_t i) const { return script_buf[i]; } - ZString get_str(size_t i) const - { - return ZString(strings::really_construct_from_a_pointer, reinterpret_cast(&script_buf[i]), nullptr); - } - - // method used elsewhere + default_delete() {} + default_delete(default_delete) {} + void operator()(const tmwa::ScriptBuffer *sd); }; +} // namespace std +namespace tmwa +{ struct ScriptPointer { const ScriptBuffer *code; @@ -116,15 +73,9 @@ struct ScriptPointer , pos(p) {} - ByteCode peek() const { return (*code)[pos]; } - ByteCode pop() { return (*code)[pos++]; } - ZString pops() - { - ZString rv = code->get_str(pos); - pos += rv.size(); - ++pos; - return rv; - } + ByteCode peek() const; + ByteCode pop(); + ZString pops(); }; // internal @@ -216,24 +167,6 @@ struct script_data : ScriptDataVariantBase script_data(ScriptDataFuncRef v) : ScriptDataVariantBase(std::move(v)) {} }; -struct script_stack -{ - std::vector stack_datav; -}; - -enum class ScriptEndState; -// future improvements coming! -class ScriptState -{ -public: - struct script_stack *stack; - int start, end; - ScriptEndState state; - BlockId rid, oid; - ScriptPointer scriptp, new_scriptp; - int defsp, new_defsp; -}; - std::unique_ptr parse_script(ZString, int, bool implicit_end); struct argrec_t -- cgit v1.2.3-60-g2f50