OpenClonk
C4AulCompiler::ConstexprEvaluator Class Reference
Inheritance diagram for C4AulCompiler::ConstexprEvaluator:
[legend]
Collaboration diagram for C4AulCompiler::ConstexprEvaluator:
[legend]

Classes

class  ExpressionNotConstant
 

Public Types

enum  EvalFlag { IgnoreUnset = 1<<0 , SuppressErrors = 1<<1 }
 
typedef int EvalFlags
 

Public Member Functions

void visit (const ::aul::ast::StringLit *n) override
 
void visit (const ::aul::ast::IntLit *n) override
 
void visit (const ::aul::ast::BoolLit *n) override
 
void visit (const ::aul::ast::ArrayLit *n) override
 
void visit (const ::aul::ast::ProplistLit *n) override
 
void visit (const ::aul::ast::NilLit *) override
 
void visit (const ::aul::ast::ThisLit *n) override
 
void visit (const ::aul::ast::VarExpr *n) override
 
void visit (const ::aul::ast::UnOpExpr *n) override
 
void visit (const ::aul::ast::BinOpExpr *n) override
 
void visit (const ::aul::ast::AssignmentExpr *n) override
 
void visit (const ::aul::ast::SubscriptExpr *n) override
 
void visit (const ::aul::ast::SliceExpr *n) override
 
void visit (const ::aul::ast::CallExpr *n) override
 
void visit (const ::aul::ast::FunctionExpr *n) override
 
virtual void visit (const ::aul::ast::Noop *)
 
virtual void visit (const ::aul::ast::ParExpr *)
 
virtual void visit (const ::aul::ast::Block *)
 
virtual void visit (const ::aul::ast::Return *)
 
virtual void visit (const ::aul::ast::ForLoop *)
 
virtual void visit (const ::aul::ast::RangeLoop *)
 
virtual void visit (const ::aul::ast::DoLoop *)
 
virtual void visit (const ::aul::ast::WhileLoop *)
 
virtual void visit (const ::aul::ast::Break *)
 
virtual void visit (const ::aul::ast::Continue *)
 
virtual void visit (const ::aul::ast::If *)
 
virtual void visit (const ::aul::ast::VarDecl *)
 
virtual void visit (const ::aul::ast::FunctionDecl *)
 
virtual void visit (const ::aul::ast::IncludePragma *)
 
virtual void visit (const ::aul::ast::AppendtoPragma *)
 
virtual void visit (const ::aul::ast::Script *)
 
template<class T >
void visit (const T *)=delete
 

Static Public Member Functions

static C4Value eval (C4ScriptHost *host, const ::aul::ast::Expr *e, EvalFlags flags=0)
 
static C4Value eval_static (C4ScriptHost *host, C4PropListStatic *parent, const std::string &parent_key, const ::aul::ast::Expr *e, EvalFlags flags=0)
 

Detailed Description

Definition at line 378 of file C4AulCompiler.cpp.

Member Typedef Documentation

◆ EvalFlags

Definition at line 392 of file C4AulCompiler.cpp.

Member Enumeration Documentation

◆ EvalFlag

Enumerator
IgnoreUnset 
SuppressErrors 

Definition at line 381 of file C4AulCompiler.cpp.

382  {
383  // If this flag is set, ConstexprEvaluator will assume unset values
384  // are nil. If it is not set, evaluation of unset values will send an
385  // ExpressionNotConstant to the error handler.
386  IgnoreUnset = 1<<0,
387  // If this flag is set, ConstexprEvaluator will not send exceptions to
388  // the error handler (so it doesn't report them twice: once from the
389  // preparsing step, then again from the compile step).
390  SuppressErrors = 1<<1
391  };

Member Function Documentation

◆ eval()

C4Value C4AulCompiler::ConstexprEvaluator::eval ( C4ScriptHost host,
const ::aul::ast::Expr e,
EvalFlags  flags = 0 
)
static

Definition at line 1816 of file C4AulCompiler.cpp.

1817 {
1818  ConstexprEvaluator ce(host);
1819  ce.ignore_unset_values = (flags & IgnoreUnset) == IgnoreUnset;
1820  try
1821  {
1822  e->accept(&ce);
1823  return ce.v;
1824  }
1825  catch (C4AulParseError &e)
1826  {
1827  if ((flags & SuppressErrors) == 0)
1828  host->Engine->ErrorHandler->OnError(e.what());
1829  return C4VNull;
1830  }
1831 }
const C4Value C4VNull
Definition: C4Value.cpp:30
virtual void OnError(const char *msg)=0
const char * what() const noexcept override
Definition: C4Aul.cpp:59
C4AulErrorHandler * ErrorHandler
Definition: C4Aul.h:128
C4AulScriptEngine * Engine
Definition: C4ScriptHost.h:77
virtual void accept(::aul::AstVisitor *) const =0

References C4VNull, C4ScriptHost::Engine, C4AulScriptEngine::ErrorHandler, and C4AulErrorHandler::OnError().

Here is the call graph for this function:

◆ eval_static()

C4Value C4AulCompiler::ConstexprEvaluator::eval_static ( C4ScriptHost host,
C4PropListStatic parent,
const std::string &  parent_key,
const ::aul::ast::Expr e,
EvalFlags  flags = 0 
)
static

Definition at line 1833 of file C4AulCompiler.cpp.

1834 {
1835  ConstexprEvaluator ce(host);
1836  ce.proplist_magic = ConstexprEvaluator::ProplistMagic{ true, parent, parent_key };
1837  ce.ignore_unset_values = (flags & IgnoreUnset) == IgnoreUnset;
1838  try
1839  {
1840  e->accept(&ce);
1841  return ce.v;
1842  }
1843  catch (C4AulParseError &e)
1844  {
1845  if ((flags & SuppressErrors) == 0)
1846  host->Engine->ErrorHandler->OnError(e.what());
1847  return C4VNull;
1848  }
1849 }

References C4VNull, C4ScriptHost::Engine, C4AulScriptEngine::ErrorHandler, and C4AulErrorHandler::OnError().

Referenced by C4AulCompiler::ConstantResolver::visit().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ visit() [1/32]

virtual void aul::AstVisitor::visit ( const ::aul::ast::AppendtoPragma )
inlinevirtualinherited

Reimplemented in C4AulCompiler::PreparseAstVisitor.

Definition at line 92 of file C4AulAST.h.

92 {}

◆ visit() [2/32]

void C4AulCompiler::ConstexprEvaluator::visit ( const ::aul::ast::ArrayLit n)
overridevirtual

Reimplemented from aul::AstVisitor.

Definition at line 1857 of file C4AulCompiler.cpp.

1858 {
1859  auto a = std::make_unique<C4ValueArray>(n->values.size());
1860  for (size_t i = 0; i < n->values.size(); ++i)
1861  {
1862  n->values[i]->accept(this);
1863  a->SetItem(i, v);
1864  }
1865  v = C4VArray(a.release());
1866 }
#define a
C4Value C4VArray(C4ValueArray *pArray)
Definition: C4Value.h:246
std::vector< ExprPtr > values
Definition: C4AulAST.h:180

References a, and C4VArray().

Here is the call graph for this function:

◆ visit() [3/32]

void C4AulCompiler::ConstexprEvaluator::visit ( const ::aul::ast::AssignmentExpr n)
overridevirtual

Reimplemented from aul::AstVisitor.

Definition at line 2049 of file C4AulCompiler.cpp.

2050 {
2051  nonconst(n, "updating assignment used in a non-const fashion");
2052 }

◆ visit() [4/32]

void C4AulCompiler::ConstexprEvaluator::visit ( const ::aul::ast::BinOpExpr n)
overridevirtual

Reimplemented from aul::AstVisitor.

Definition at line 1976 of file C4AulCompiler.cpp.

1977 {
1978  assert(n->op > 0);
1979  const auto &op = C4ScriptOpMap[n->op];
1980  if (op.Changer)
1981  nonconst(n, "binary operator %s is applied in a non-const fashion", op.Identifier);
1982 
1983  n->lhs->accept(this);
1984  C4Value lhs = v;
1985  // Evaluate the short-circuiting operators here
1986  if ((op.Code == AB_JUMPAND && !lhs) || (op.Code == AB_JUMPOR && lhs) || (op.Code == AB_JUMPNNIL && lhs.GetType() != C4V_Nil))
1987  {
1988  v = lhs;
1989  return;
1990  }
1991  n->rhs->accept(this);
1992  C4Value &rhs = v;
1993 
1994  AssertValueType(lhs, op.Type1, op.Identifier, n);
1995  AssertValueType(rhs, op.Type2, op.Identifier, n);
1996 
1997  switch (op.Code)
1998  {
1999  case AB_Pow:
2000  v.SetInt(Pow(lhs._getInt(), rhs._getInt()));
2001  break;
2002  case AB_Div:
2003  ENSURE_COND(rhs._getInt() != 0, "division by zero");
2004  ENSURE_COND(lhs._getInt() != INT32_MIN || rhs._getInt() != -1, "division overflow");
2005  v.SetInt(lhs._getInt() / rhs._getInt());
2006  break;
2007  case AB_Mul:
2008  v.SetInt(lhs._getInt() * rhs._getInt());
2009  break;
2010  case AB_Mod:
2011  ENSURE_COND(rhs._getInt() != 0, "division by zero");
2012  ENSURE_COND(lhs._getInt() != INT32_MIN || rhs._getInt() != -1, "division overflow");
2013  v.SetInt(lhs._getInt() / rhs._getInt());
2014  break;
2015 #define INT_BINOP(code, op) case code: v.SetInt(lhs._getInt() op rhs._getInt()); break
2016  INT_BINOP(AB_Sum, +);
2017  INT_BINOP(AB_Sub, -);
2018  INT_BINOP(AB_LeftShift, << );
2019  INT_BINOP(AB_RightShift, >> );
2020  INT_BINOP(AB_BitAnd, &);
2021  INT_BINOP(AB_BitXOr, ^);
2022  INT_BINOP(AB_BitOr, | );
2023 #undef INT_BINOP
2024 #define BOOL_BINOP(code, op) case code: v.SetBool(lhs._getInt() op rhs._getInt()); break
2025  BOOL_BINOP(AB_LessThan, <);
2029 #undef BOOL_BINOP
2030  case AB_Equal:
2031  v.SetBool(lhs.IsIdenticalTo(rhs));
2032  break;
2033  case AB_NotEqual:
2034  v.SetBool(!lhs.IsIdenticalTo(rhs));
2035  break;
2036  case AB_JUMPAND:
2037  case AB_JUMPOR:
2038  case AB_JUMPNNIL:
2039  // If we hit this, then the short-circuit above failed
2040  v = rhs;
2041  break;
2042  default:
2043  assert(!"ConstexprEvaluator: Unexpected binary operator");
2044  throw Error(host, host, n, nullptr, "internal error: binary operator not found in operator table");
2045  break;
2046  }
2047 }
#define INT_BINOP(code, op)
#define ENSURE_COND(cond, failmsg)
#define BOOL_BINOP(code, op)
const C4ScriptOpDef C4ScriptOpMap[]
Definition: C4AulParse.cpp:275
@ AB_Pow
@ AB_BitAnd
@ AB_LeftShift
@ AB_Sum
@ AB_GreaterThanEqual
@ AB_Mul
@ AB_Sub
@ AB_LessThan
@ AB_LessThanEqual
@ AB_RightShift
@ AB_GreaterThan
@ AB_Equal
@ AB_BitOr
@ AB_Mod
@ AB_NotEqual
@ AB_Div
@ AB_JUMPAND
@ AB_JUMPNNIL
@ AB_JUMPOR
@ AB_BitXOr
@ C4V_Nil
Definition: C4Value.h:25
int Pow(int base, int exponent)
Definition: Standard.cpp:69
void SetBool(bool b)
Definition: C4Value.h:137
C4V_Type GetType() const
Definition: C4Value.h:161
int32_t _getInt() const
Definition: C4Value.h:122
void SetInt(int32_t i)
Definition: C4Value.h:136
bool IsIdenticalTo(const C4Value &cmp) const
Definition: C4Value.h:149

References C4Value::_getInt(), AB_BitAnd, AB_BitOr, AB_BitXOr, AB_Div, AB_Equal, AB_GreaterThan, AB_GreaterThanEqual, AB_JUMPAND, AB_JUMPNNIL, AB_JUMPOR, AB_LeftShift, AB_LessThan, AB_LessThanEqual, AB_Mod, AB_Mul, AB_NotEqual, AB_Pow, AB_RightShift, AB_Sub, AB_Sum, BOOL_BINOP, C4ScriptOpMap, C4V_Nil, ENSURE_COND, C4Value::GetType(), INT_BINOP, C4Value::IsIdenticalTo(), Pow(), C4Value::SetBool(), and C4Value::SetInt().

Here is the call graph for this function:

◆ visit() [5/32]

virtual void aul::AstVisitor::visit ( const ::aul::ast::Block )
inlinevirtualinherited

Reimplemented in C4AulCompiler::CodegenAstVisitor, and aul::DefaultRecursiveVisitor.

Definition at line 79 of file C4AulAST.h.

79 {}

◆ visit() [6/32]

void C4AulCompiler::ConstexprEvaluator::visit ( const ::aul::ast::BoolLit n)
overridevirtual

Reimplemented from aul::AstVisitor.

Definition at line 1855 of file C4AulCompiler.cpp.

1855 { v = C4VBool(n->value); }
C4Value C4VBool(bool b)
Definition: C4Value.h:240

References C4VBool().

Here is the call graph for this function:

◆ visit() [7/32]

virtual void aul::AstVisitor::visit ( const ::aul::ast::Break )
inlinevirtualinherited

Reimplemented in C4AulCompiler::CodegenAstVisitor.

Definition at line 85 of file C4AulAST.h.

85 {}

◆ visit() [8/32]

void C4AulCompiler::ConstexprEvaluator::visit ( const ::aul::ast::CallExpr n)
overridevirtual

Reimplemented from aul::AstVisitor.

Definition at line 2094 of file C4AulCompiler.cpp.

2095 {
2096  // TODO: allow side-effect-free calls here
2097  nonconst(n, "call to function (%s) not supported in constant expressions", n->callee.c_str());
2098 }
std::string callee
Definition: C4AulAST.h:258

◆ visit() [9/32]

virtual void aul::AstVisitor::visit ( const ::aul::ast::Continue )
inlinevirtualinherited

Reimplemented in C4AulCompiler::CodegenAstVisitor.

Definition at line 86 of file C4AulAST.h.

86 {}

◆ visit() [10/32]

virtual void aul::AstVisitor::visit ( const ::aul::ast::DoLoop )
inlinevirtualinherited

Reimplemented in C4AulCompiler::CodegenAstVisitor, and aul::DefaultRecursiveVisitor.

Definition at line 83 of file C4AulAST.h.

83 {}

◆ visit() [11/32]

virtual void aul::AstVisitor::visit ( const ::aul::ast::ForLoop )
inlinevirtualinherited

Reimplemented in C4AulCompiler::CodegenAstVisitor, and aul::DefaultRecursiveVisitor.

Definition at line 81 of file C4AulAST.h.

81 {}

◆ visit() [12/32]

virtual void aul::AstVisitor::visit ( const ::aul::ast::FunctionDecl )
inlinevirtualinherited

◆ visit() [13/32]

void C4AulCompiler::ConstexprEvaluator::visit ( const ::aul::ast::FunctionExpr n)
overridevirtual

Reimplemented from aul::AstVisitor.

Definition at line 2100 of file C4AulCompiler.cpp.

2101 {
2102  // Function expressions can only occur inside static proplists.
2103  ENSURE_COND(proplist_magic.active, "internal error: function expression outside of static proplist");
2104 
2105  C4AulScriptFunc *sfunc = nullptr;
2106  bool first_pass = true;
2107 
2108  if (auto func = proplist_magic.parent->GetFunc(proplist_magic.key.c_str()))
2109  {
2110  sfunc = func->SFunc();
2111  first_pass = false;
2112  }
2113  else
2114  {
2115  sfunc = new C4AulScriptFunc(proplist_magic.parent, host, proplist_magic.key.c_str(), n->loc);
2116  }
2117 
2118  ENSURE_COND(sfunc != nullptr, "internal error: function expression target resolved to non-function value");
2119 
2120  if (first_pass)
2121  {
2122  for (const auto &param : n->params)
2123  {
2124  sfunc->AddPar(param.name.c_str());
2125  }
2126  if (n->has_unnamed_params)
2127  sfunc->ParCount = C4AUL_MAX_Par;
2128 
2129  PreparseAstVisitor preparser(host, host, sfunc);
2130  preparser.visit(n->body.get());
2131  }
2132  else
2133  {
2134  CodegenAstVisitor cg(sfunc);
2135  cg.EmitFunctionCode(n);
2136  }
2137 
2138  v.SetFunction(sfunc);
2139 }
#define C4AUL_MAX_Par
Definition: C4AulFunc.h:26
void AddPar(const char *Idtf, C4V_Type type=C4V_Any)
C4AulScriptFunc * SFunc() override
void SetFunction(C4AulFunc *Fn)
Definition: C4Value.h:140
std::vector< Parameter > params
Definition: C4AulAST.h:377
bool has_unnamed_params
Definition: C4AulAST.h:378
std::unique_ptr< Block > body
Definition: C4AulAST.h:379
const char * loc
Definition: C4AulAST.h:123

References C4AulScriptFunc::AddPar(), C4AUL_MAX_Par, C4AulCompiler::CodegenAstVisitor::EmitFunctionCode(), ENSURE_COND, C4AulScriptFunc::ParCount, C4Value::SetFunction(), C4AulScriptFunc::SFunc(), and C4AulCompiler::PreparseAstVisitor::visit().

Here is the call graph for this function:

◆ visit() [14/32]

virtual void aul::AstVisitor::visit ( const ::aul::ast::If )
inlinevirtualinherited

Reimplemented in C4AulCompiler::CodegenAstVisitor, and aul::DefaultRecursiveVisitor.

Definition at line 87 of file C4AulAST.h.

87 {}

◆ visit() [15/32]

virtual void aul::AstVisitor::visit ( const ::aul::ast::IncludePragma )
inlinevirtualinherited

Reimplemented in C4AulCompiler::PreparseAstVisitor.

Definition at line 91 of file C4AulAST.h.

91 {}

◆ visit() [16/32]

void C4AulCompiler::ConstexprEvaluator::visit ( const ::aul::ast::IntLit n)
overridevirtual

Reimplemented from aul::AstVisitor.

Definition at line 1853 of file C4AulCompiler.cpp.

1853 { v = C4VInt(n->value); }
C4Value C4VInt(int32_t i)
Definition: C4Value.h:239
uint32_t value
Definition: C4AulAST.h:165

References C4VInt().

Here is the call graph for this function:

◆ visit() [17/32]

void C4AulCompiler::ConstexprEvaluator::visit ( const ::aul::ast::NilLit )
overridevirtual

Reimplemented from aul::AstVisitor.

Definition at line 1929 of file C4AulCompiler.cpp.

1929 { v = C4VNull; }

References C4VNull.

◆ visit() [18/32]

virtual void aul::AstVisitor::visit ( const ::aul::ast::Noop )
inlinevirtualinherited

Reimplemented in C4AulCompiler::CodegenAstVisitor.

Definition at line 63 of file C4AulAST.h.

63 {}

◆ visit() [19/32]

virtual void aul::AstVisitor::visit ( const ::aul::ast::ParExpr )
inlinevirtualinherited

◆ visit() [20/32]

void C4AulCompiler::ConstexprEvaluator::visit ( const ::aul::ast::ProplistLit n)
overridevirtual

Reimplemented from aul::AstVisitor.

Definition at line 1868 of file C4AulCompiler.cpp.

1869 {
1870  std::unique_ptr<C4PropList> new_proplist;
1871  C4PropList *p = nullptr;
1872 
1873  bool first_pass = true;
1874 
1875  if (proplist_magic.active)
1876  {
1877  // Check if there's already a proplist available
1878  C4String *key = ::Strings.RegString(proplist_magic.key.c_str());
1879  C4Value old;
1880  if (proplist_magic.parent)
1881  {
1882  proplist_magic.parent->GetPropertyByS(key, &old);
1883  }
1884  else
1885  {
1886  // If proplist_magic.parent is nullptr, we're handling a global constant.
1887  host->Engine->GetGlobalConstant(key->GetCStr(), &old);
1888  }
1889  if (old.getPropList())
1890  {
1891  p = old.getPropList();
1892  first_pass = false;
1893  }
1894  else
1895  {
1896  p = C4PropList::NewStatic(nullptr, proplist_magic.parent, key);
1897  new_proplist.reset(p);
1898  }
1899  }
1900  else
1901  {
1902  p = C4PropList::New();
1903  new_proplist.reset(p);
1904  }
1905 
1906  // Since the values may be functions that refer to other values in the
1907  // proplist, pre-populate the new proplist with dummy values until the
1908  // real ones are set
1909  if (first_pass)
1910  {
1911  for (const auto &kv : n->values)
1912  {
1913  p->SetPropertyByS(::Strings.RegString(kv.first.c_str()), C4VNull);
1914  }
1915  }
1916 
1917  auto saved_magic = std::move(proplist_magic);
1918  for (const auto &kv : n->values)
1919  {
1920  proplist_magic = ProplistMagic { saved_magic.active, p->IsStatic(), kv.first };
1921  kv.second->accept(this);
1922  p->SetPropertyByS(::Strings.RegString(kv.first.c_str()), v);
1923  }
1924  proplist_magic = std::move(saved_magic);
1925  v = C4VPropList(p);
1926  new_proplist.release();
1927 }
C4StringTable Strings
Definition: C4Globals.cpp:42
C4Value C4VPropList(C4PropList *p)
Definition: C4Value.h:242
bool GetGlobalConstant(const char *szName, C4Value *pTargetValue)
Definition: C4Aul.cpp:133
virtual class C4PropListStatic * IsStatic()
Definition: C4PropList.h:89
virtual void SetPropertyByS(C4String *k, const C4Value &to)
Definition: C4PropList.cpp:940
static C4PropList * New(C4PropList *prototype=nullptr)
Definition: C4PropList.cpp:40
static C4PropListStatic * NewStatic(C4PropList *prototype, const C4PropListStatic *parent, C4String *key)
Definition: C4PropList.cpp:46
const char * GetCStr() const
Definition: C4StringTable.h:49
C4String * RegString(StdStrBuf String)
C4PropList * getPropList() const
Definition: C4Value.h:116
std::vector< std::pair< std::string, ExprPtr > > values
Definition: C4AulAST.h:187

References C4VNull, C4VPropList(), C4ScriptHost::Engine, C4String::GetCStr(), C4AulScriptEngine::GetGlobalConstant(), C4Value::getPropList(), C4PropList::IsStatic(), C4PropList::New(), C4PropList::NewStatic(), C4StringTable::RegString(), C4PropList::SetPropertyByS(), and Strings.

Here is the call graph for this function:

◆ visit() [21/32]

virtual void aul::AstVisitor::visit ( const ::aul::ast::RangeLoop )
inlinevirtualinherited

◆ visit() [22/32]

virtual void aul::AstVisitor::visit ( const ::aul::ast::Return )
inlinevirtualinherited

Reimplemented in C4AulCompiler::CodegenAstVisitor, and aul::DefaultRecursiveVisitor.

Definition at line 80 of file C4AulAST.h.

80 {}

◆ visit() [23/32]

virtual void aul::AstVisitor::visit ( const ::aul::ast::Script )
inlinevirtualinherited

◆ visit() [24/32]

void C4AulCompiler::ConstexprEvaluator::visit ( const ::aul::ast::SliceExpr n)
overridevirtual

Reimplemented from aul::AstVisitor.

Definition at line 2078 of file C4AulCompiler.cpp.

2079 {
2080  n->object->accept(this);
2081  C4Value obj = v;
2082  n->start->accept(this);
2083  C4Value start = v;
2084  n->end->accept(this);
2085  C4Value &end = v;
2086 
2087  ENSURE_COND(obj.CheckConversion(C4V_Array), FormatString("array slice: can't access %s as an array", obj.GetTypeName()).getData());
2088  ENSURE_COND(start.CheckConversion(C4V_Int), FormatString("array slice: start index of type %s, int expected", start.GetTypeName()).getData());
2089  ENSURE_COND(end.CheckConversion(C4V_Int), FormatString("array slice: end index of type %s, int expected", end.GetTypeName()).getData());
2090 
2091  v.SetArray(obj.getArray()->GetSlice(start.getInt(), end.getInt()));
2092 }
@ C4V_Int
Definition: C4Value.h:26
@ C4V_Array
Definition: C4Value.h:30
StdStrBuf FormatString(const char *szFmt,...)
Definition: StdBuf.cpp:270
C4ValueArray * GetSlice(int32_t startIndex, int32_t endIndex)
C4ValueArray * getArray() const
Definition: C4Value.h:118
ALWAYS_INLINE bool CheckConversion(C4V_Type vtToType) const
Definition: C4Value.h:189
int32_t getInt() const
Definition: C4Value.h:112
void SetArray(C4ValueArray *Array)
Definition: C4Value.h:139
const char * GetTypeName() const
Definition: C4Value.h:164
const char * getData() const
Definition: StdBuf.h:442

References C4V_Array, C4V_Int, C4Value::CheckConversion(), ENSURE_COND, FormatString(), C4Value::getArray(), StdStrBuf::getData(), C4Value::getInt(), C4ValueArray::GetSlice(), C4Value::GetTypeName(), and C4Value::SetArray().

Here is the call graph for this function:

◆ visit() [25/32]

void C4AulCompiler::ConstexprEvaluator::visit ( const ::aul::ast::StringLit n)
overridevirtual

Reimplemented from aul::AstVisitor.

Definition at line 1851 of file C4AulCompiler.cpp.

1851 { v = C4VString(n->value.c_str()); }
C4Value C4VString(C4String *pStr)
Definition: C4Value.h:243
std::string value
Definition: C4AulAST.h:157

References C4VString().

Here is the call graph for this function:

◆ visit() [26/32]

void C4AulCompiler::ConstexprEvaluator::visit ( const ::aul::ast::SubscriptExpr n)
overridevirtual

Reimplemented from aul::AstVisitor.

Definition at line 2054 of file C4AulCompiler.cpp.

2055 {
2056  n->object->accept(this);
2057  C4Value obj = v;
2058  n->index->accept(this);
2059  C4Value &index = v;
2060 
2061  if (obj.CheckConversion(C4V_Array))
2062  {
2063  ENSURE_COND(index.CheckConversion(C4V_Int), FormatString("array access: index of type %s, but expected int", index.GetTypeName()).getData());
2064  v = obj.getArray()->GetItem(index.getInt());
2065  }
2066  else if (obj.CheckConversion(C4V_PropList))
2067  {
2068  ENSURE_COND(index.CheckConversion(C4V_String), FormatString("proplist access: index of type %s, but expected string", index.GetTypeName()).getData());
2069  if (!obj.getPropList()->GetPropertyByS(index.getStr(), &v))
2070  v.Set0();
2071  }
2072  else
2073  {
2074  ENSURE_COND(false, FormatString("can't access %s as array or proplist", obj.GetTypeName()).getData());
2075  }
2076 }
@ C4V_PropList
Definition: C4Value.h:28
@ C4V_String
Definition: C4Value.h:29
virtual bool GetPropertyByS(const C4String *k, C4Value *pResult) const
Definition: C4PropList.cpp:726
const C4Value & GetItem(int32_t iElem) const
Definition: C4ValueArray.h:38
C4String * getStr() const
Definition: C4Value.h:117
void Set0()
Definition: C4Value.h:332

References C4V_Array, C4V_Int, C4V_PropList, C4V_String, C4Value::CheckConversion(), ENSURE_COND, FormatString(), C4Value::getArray(), StdStrBuf::getData(), C4Value::getInt(), C4ValueArray::GetItem(), C4PropList::GetPropertyByS(), C4Value::getPropList(), C4Value::getStr(), C4Value::GetTypeName(), and C4Value::Set0().

Here is the call graph for this function:

◆ visit() [27/32]

void C4AulCompiler::ConstexprEvaluator::visit ( const ::aul::ast::ThisLit n)
overridevirtual

Reimplemented from aul::AstVisitor.

Definition at line 1931 of file C4AulCompiler.cpp.

1931 { nonconst(n, "\"this\" is not a global constant"); }

◆ visit() [28/32]

void C4AulCompiler::ConstexprEvaluator::visit ( const ::aul::ast::UnOpExpr n)
overridevirtual

Reimplemented from aul::AstVisitor.

Definition at line 1951 of file C4AulCompiler.cpp.

1952 {
1953  n->operand->accept(this);
1954  assert(n->op > 0);
1955  const auto &op = C4ScriptOpMap[n->op];
1956  if (op.Changer)
1957  nonconst(n, "unary operator %s is applied in a non-const fashion", op.Identifier);
1958  AssertValueType(v, op.Type1, op.Identifier, n);
1959  switch (op.Code)
1960  {
1961  case AB_BitNot:
1962  v.SetInt(~v._getInt());
1963  break;
1964  case AB_Not:
1965  v.SetBool(!v.getBool());
1966  break;
1967  case AB_Neg:
1968  v.SetInt(-v._getInt());
1969  break;
1970  default:
1971  assert(!"ConstexprEvaluator: Unexpected unary operator");
1972  throw Error(host, host, n, nullptr, "internal error: unary operator not found in operator table");
1973  }
1974 }
@ AB_BitNot
@ AB_Neg
@ AB_Not
bool getBool() const
Definition: C4Value.h:113

References C4Value::_getInt(), AB_BitNot, AB_Neg, AB_Not, C4ScriptOpMap, C4Value::getBool(), C4Value::SetBool(), and C4Value::SetInt().

Here is the call graph for this function:

◆ visit() [29/32]

virtual void aul::AstVisitor::visit ( const ::aul::ast::VarDecl )
inlinevirtualinherited

◆ visit() [30/32]

void C4AulCompiler::ConstexprEvaluator::visit ( const ::aul::ast::VarExpr n)
overridevirtual

Reimplemented from aul::AstVisitor.

Definition at line 1933 of file C4AulCompiler.cpp.

1934 {
1935  const char *cname = n->identifier.c_str();
1936  C4String *interned = ::Strings.FindString(cname);
1937  if (interned && host->GetPropList()->GetPropertyByS(interned, &v))
1938  return;
1939  if (host->Engine->GetGlobalConstant(cname, &v))
1940  return;
1941 
1942  if (ignore_unset_values)
1943  {
1944  v = C4VNull;
1945  return;
1946  }
1947 
1948  nonconst(n, "the variable \"%s\" is not a global constant", cname);
1949 }
virtual C4PropListStatic * GetPropList()
Definition: C4ScriptHost.h:51
C4String * FindString(const char *strString) const
std::string identifier
Definition: C4AulAST.h:205

References C4VNull, C4ScriptHost::Engine, C4StringTable::FindString(), C4AulScriptEngine::GetGlobalConstant(), C4PropList::GetPropertyByS(), C4ScriptHost::GetPropList(), and Strings.

Here is the call graph for this function:

◆ visit() [31/32]

virtual void aul::AstVisitor::visit ( const ::aul::ast::WhileLoop )
inlinevirtualinherited

Reimplemented in C4AulCompiler::CodegenAstVisitor, and aul::DefaultRecursiveVisitor.

Definition at line 84 of file C4AulAST.h.

84 {}

◆ visit() [32/32]

template<class T >
void aul::AstVisitor::visit ( const T *  )
deleteinherited

The documentation for this class was generated from the following file: