1 // -*- C++ -*-
2 // System intended to replace the need for internal global state.
3 
4 #ifndef EXPRESSION_H
5 #define EXPRESSION_H
6 
7 #include "defs.h"
8 
9 class ScriptHandler;
10 
11 class Expression {
12 public:
13     enum type_t { Int, Array, String, Label, Bareword };
type()14     type_t type() const { return type_; }
15 
16     // For debugging use
17     pstring debug_string() const; // returns representation such as "?25[0]"
18 
19     // Test for attributes
is_variable()20     bool is_variable() const { return var_; }
is_constant()21     bool is_constant() const { return !var_; }
is_textual()22     bool is_textual() const
23 	{ return type_ == String || type_ == Label || type_ == Bareword; }
is_numeric()24     bool is_numeric() const
25 	{ return type_ == Int || type_ == Array; }
is_array()26     bool is_array() const { return type_ == Array; }
is_label()27     bool is_label() const { return type_ == Label; }
is_bareword()28     bool is_bareword() const { return type_ == Bareword; }
29     bool is_bareword(pstring s) const;
30 
31     // Fail if attributes are missing
32     void require(type_t t) const;
33     void require(type_t t, bool var) const;
34     void require_variable() const;
35     void require_textual() const;
36     void require_numeric() const;
require_label()37     void require_label()    const { require(Label); }
require_bareword()38     void require_bareword() const { require(Bareword); }
39 
40     // Access contents
41     pstring as_string() const; // coerced if necessary
42     int as_int() const; // coerced if necessary
43     int var_no() const; // if variable
44     int dim() const; // if array: returns size of indexed dimension
45 
46     // Modify variable
47     void mutate(int newval, int offset = MAX_INT, bool as_array = false);
48     void mutate(const pstring& newval);
49     void append(const pstring& newval);
50     void append(wchar newval);
51 
52     Expression(ScriptHandler& sh);
53     Expression(ScriptHandler& sh, type_t t, bool is_v, int val);
54     Expression(ScriptHandler& sh, type_t t, bool is_v, int val,
55 	       const h_index_t& idx);
56     Expression(ScriptHandler& sh, type_t t, bool is_v, const pstring& val);
57 
58     Expression& operator=(const Expression& src);
59 private:
60     void die(const char* why) const;
61     ScriptHandler& h;
62     type_t type_;
63     bool var_;
64     h_index_t index_;
65     pstring strval_;
66     int intval_;
67 };
68 
69 #endif
70