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 317 of file C4AulCompiler.cpp.

Member Typedef Documentation

◆ EvalFlags

Definition at line 331 of file C4AulCompiler.cpp.

Member Enumeration Documentation

◆ EvalFlag

Enumerator
IgnoreUnset 
SuppressErrors 

Definition at line 320 of file C4AulCompiler.cpp.

321  {
322  // If this flag is set, ConstexprEvaluator will assume unset values
323  // are nil. If it is not set, evaluation of unset values will send an
324  // ExpressionNotConstant to the error handler.
325  IgnoreUnset = 1<<0,
326  // If this flag is set, ConstexprEvaluator will not send exceptions to
327  // the error handler (so it doesn't report them twice: once from the
328  // preparsing step, then again from the compile step).
329  SuppressErrors = 1<<1
330  };

Member Function Documentation

◆ eval()

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

Definition at line 1729 of file C4AulCompiler.cpp.

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

1730 {
1731  ConstexprEvaluator ce(host);
1732  ce.ignore_unset_values = (flags & IgnoreUnset) == IgnoreUnset;
1733  try
1734  {
1735  e->accept(&ce);
1736  return ce.v;
1737  }
1738  catch (C4AulParseError &e)
1739  {
1740  if ((flags & SuppressErrors) == 0)
1741  host->Engine->ErrorHandler->OnError(e.what());
1742  return C4VNull;
1743  }
1744 }
C4AulScriptEngine * Engine
Definition: C4ScriptHost.h:76
const char * what() const noexcept override
Definition: C4Aul.cpp:59
virtual void OnError(const char *msg)=0
const C4Value C4VNull
Definition: C4Value.cpp:30
virtual void accept(::aul::AstVisitor *) const =0
C4AulErrorHandler * ErrorHandler
Definition: C4Aul.h:128
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 1746 of file C4AulCompiler.cpp.

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

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

1747 {
1748  ConstexprEvaluator ce(host);
1749  ce.proplist_magic = ConstexprEvaluator::ProplistMagic{ true, parent, parent_key };
1750  ce.ignore_unset_values = (flags & IgnoreUnset) == IgnoreUnset;
1751  try
1752  {
1753  e->accept(&ce);
1754  return ce.v;
1755  }
1756  catch (C4AulParseError &e)
1757  {
1758  if ((flags & SuppressErrors) == 0)
1759  host->Engine->ErrorHandler->OnError(e.what());
1760  return C4VNull;
1761  }
1762 }
C4AulScriptEngine * Engine
Definition: C4ScriptHost.h:76
const char * what() const noexcept override
Definition: C4Aul.cpp:59
virtual void OnError(const char *msg)=0
const C4Value C4VNull
Definition: C4Value.cpp:30
virtual void accept(::aul::AstVisitor *) const =0
C4AulErrorHandler * ErrorHandler
Definition: C4Aul.h:128
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::Noop )
inlinevirtualinherited

Reimplemented in C4AulCompiler::CodegenAstVisitor.

Definition at line 63 of file C4AulAST.h.

63 {}

◆ visit() [2/32]

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

◆ visit() [3/32]

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

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

Definition at line 79 of file C4AulAST.h.

79 {}

◆ visit() [4/32]

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

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

Definition at line 80 of file C4AulAST.h.

80 {}

◆ visit() [5/32]

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

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

Definition at line 81 of file C4AulAST.h.

81 {}

◆ visit() [6/32]

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

◆ visit() [7/32]

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

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

Definition at line 83 of file C4AulAST.h.

83 {}

◆ visit() [8/32]

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

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

Definition at line 84 of file C4AulAST.h.

84 {}

◆ visit() [9/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() [10/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() [11/32]

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

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

Definition at line 87 of file C4AulAST.h.

87 {}

◆ visit() [12/32]

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

◆ visit() [13/32]

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

◆ visit() [14/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() [15/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() [16/32]

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

◆ visit() [17/32]

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

◆ visit() [18/32]

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

Reimplemented from aul::AstVisitor.

Definition at line 1764 of file C4AulCompiler.cpp.

References C4VString().

1764 { v = C4VString(n->value.c_str()); }
std::string value
Definition: C4AulAST.h:157
C4Value C4VString(C4String *pStr)
Definition: C4Value.h:246
Here is the call graph for this function:

◆ visit() [19/32]

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

Reimplemented from aul::AstVisitor.

Definition at line 1766 of file C4AulCompiler.cpp.

References C4VInt().

1766 { v = C4VInt(n->value); }
C4Value C4VInt(int32_t i)
Definition: C4Value.h:242
uint32_t value
Definition: C4AulAST.h:165
Here is the call graph for this function:

◆ visit() [20/32]

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

Reimplemented from aul::AstVisitor.

Definition at line 1768 of file C4AulCompiler.cpp.

References C4VBool().

1768 { v = C4VBool(n->value); }
C4Value C4VBool(bool b)
Definition: C4Value.h:243
Here is the call graph for this function:

◆ visit() [21/32]

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

Reimplemented from aul::AstVisitor.

Definition at line 1770 of file C4AulCompiler.cpp.

References a, and C4VArray().

1771 {
1772  auto a = std::make_unique<C4ValueArray>(n->values.size());
1773  for (size_t i = 0; i < n->values.size(); ++i)
1774  {
1775  n->values[i]->accept(this);
1776  a->SetItem(i, v);
1777  }
1778  v = C4VArray(a.release());
1779 }
#define a
std::vector< ExprPtr > values
Definition: C4AulAST.h:180
C4Value C4VArray(C4ValueArray *pArray)
Definition: C4Value.h:249
Here is the call graph for this function:

◆ visit() [22/32]

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

Reimplemented from aul::AstVisitor.

Definition at line 1781 of file C4AulCompiler.cpp.

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

1782 {
1783  std::unique_ptr<C4PropList> new_proplist;
1784  C4PropList *p = nullptr;
1785 
1786  bool first_pass = true;
1787 
1788  if (proplist_magic.active)
1789  {
1790  // Check if there's already a proplist available
1791  C4String *key = ::Strings.RegString(proplist_magic.key.c_str());
1792  C4Value old;
1793  if (proplist_magic.parent)
1794  {
1795  proplist_magic.parent->GetPropertyByS(key, &old);
1796  }
1797  else
1798  {
1799  // If proplist_magic.parent is nullptr, we're handling a global constant.
1800  host->Engine->GetGlobalConstant(key->GetCStr(), &old);
1801  }
1802  if (old.getPropList())
1803  {
1804  p = old.getPropList();
1805  first_pass = false;
1806  }
1807  else
1808  {
1809  p = C4PropList::NewStatic(nullptr, proplist_magic.parent, key);
1810  new_proplist.reset(p);
1811  }
1812  }
1813  else
1814  {
1815  p = C4PropList::New();
1816  new_proplist.reset(p);
1817  }
1818 
1819  // Since the values may be functions that refer to other values in the
1820  // proplist, pre-populate the new proplist with dummy values until the
1821  // real ones are set
1822  if (first_pass)
1823  {
1824  for (const auto &kv : n->values)
1825  {
1826  p->SetPropertyByS(::Strings.RegString(kv.first.c_str()), C4VNull);
1827  }
1828  }
1829 
1830  auto saved_magic = std::move(proplist_magic);
1831  for (const auto &kv : n->values)
1832  {
1833  proplist_magic = ProplistMagic { saved_magic.active, p->IsStatic(), kv.first };
1834  kv.second->accept(this);
1835  p->SetPropertyByS(::Strings.RegString(kv.first.c_str()), v);
1836  }
1837  proplist_magic = std::move(saved_magic);
1838  v = C4VPropList(p);
1839  new_proplist.release();
1840 }
C4PropList * getPropList() const
Definition: C4Value.h:116
C4String * RegString(StdStrBuf String)
bool GetGlobalConstant(const char *szName, C4Value *pTargetValue)
Definition: C4Aul.cpp:133
virtual class C4PropListStatic * IsStatic()
Definition: C4PropList.h:85
C4AulScriptEngine * Engine
Definition: C4ScriptHost.h:76
const char * GetCStr() const
Definition: C4StringTable.h:49
C4StringTable Strings
Definition: C4Globals.cpp:42
C4Value C4VPropList(C4PropList *p)
Definition: C4Value.h:245
static C4PropListStatic * NewStatic(C4PropList *prototype, const C4PropListStatic *parent, C4String *key)
Definition: C4PropList.cpp:70
const C4Value C4VNull
Definition: C4Value.cpp:30
virtual void SetPropertyByS(C4String *k, const C4Value &to)
Definition: C4PropList.cpp:971
std::vector< std::pair< std::string, ExprPtr > > values
Definition: C4AulAST.h:187
static C4PropList * New(C4PropList *prototype=nullptr)
Definition: C4PropList.cpp:64
Here is the call graph for this function:

◆ visit() [23/32]

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

Reimplemented from aul::AstVisitor.

Definition at line 1842 of file C4AulCompiler.cpp.

References C4VNull.

1842 { v = C4VNull; }
const C4Value C4VNull
Definition: C4Value.cpp:30

◆ visit() [24/32]

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

Reimplemented from aul::AstVisitor.

Definition at line 1844 of file C4AulCompiler.cpp.

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

◆ visit() [25/32]

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

Reimplemented from aul::AstVisitor.

Definition at line 1846 of file C4AulCompiler.cpp.

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

1847 {
1848  const char *cname = n->identifier.c_str();
1849  C4String *interned = ::Strings.FindString(cname);
1850  if (interned && host->GetPropList()->GetPropertyByS(interned, &v))
1851  return;
1852  if (host->Engine->GetGlobalConstant(cname, &v))
1853  return;
1854 
1855  if (ignore_unset_values)
1856  {
1857  v = C4VNull;
1858  return;
1859  }
1860 
1861  nonconst(n, "the variable \"%s\" is not a global constant", cname);
1862 }
bool GetGlobalConstant(const char *szName, C4Value *pTargetValue)
Definition: C4Aul.cpp:133
C4AulScriptEngine * Engine
Definition: C4ScriptHost.h:76
virtual C4PropListStatic * GetPropList()
Definition: C4ScriptHost.h:50
C4StringTable Strings
Definition: C4Globals.cpp:42
const C4Value C4VNull
Definition: C4Value.cpp:30
C4String * FindString(const char *strString) const
std::string identifier
Definition: C4AulAST.h:205
virtual bool GetPropertyByS(const C4String *k, C4Value *pResult) const
Definition: C4PropList.cpp:757
Here is the call graph for this function:

◆ visit() [26/32]

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

Reimplemented from aul::AstVisitor.

Definition at line 1864 of file C4AulCompiler.cpp.

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

1865 {
1866  n->operand->accept(this);
1867  assert(n->op > 0);
1868  const auto &op = C4ScriptOpMap[n->op];
1869  if (op.Changer)
1870  nonconst(n, "unary operator %s is applied in a non-const fashion", op.Identifier);
1871  AssertValueType(v, op.Type1, op.Identifier, n);
1872  switch (op.Code)
1873  {
1874  case AB_BitNot:
1875  v.SetInt(~v._getInt());
1876  break;
1877  case AB_Not:
1878  v.SetBool(!v.getBool());
1879  break;
1880  case AB_Neg:
1881  v.SetInt(-v._getInt());
1882  break;
1883  default:
1884  assert(!"ConstexprEvaluator: Unexpected unary operator");
1885  throw Error(host, host, n, nullptr, "internal error: unary operator not found in operator table");
1886  }
1887 }
const C4ScriptOpDef C4ScriptOpMap[]
Definition: C4AulParse.cpp:274
void SetBool(bool b)
Definition: C4Value.h:137
void SetInt(int32_t i)
Definition: C4Value.h:136
int32_t _getInt() const
Definition: C4Value.h:122
bool getBool() const
Definition: C4Value.h:113
Here is the call graph for this function:

◆ visit() [27/32]

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

Reimplemented from aul::AstVisitor.

Definition at line 1889 of file C4AulCompiler.cpp.

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().

1890 {
1891  assert(n->op > 0);
1892  const auto &op = C4ScriptOpMap[n->op];
1893  if (op.Changer)
1894  nonconst(n, "binary operator %s is applied in a non-const fashion", op.Identifier);
1895 
1896  n->lhs->accept(this);
1897  C4Value lhs = v;
1898  // Evaluate the short-circuiting operators here
1899  if ((op.Code == AB_JUMPAND && !lhs) || (op.Code == AB_JUMPOR && lhs) || (op.Code == AB_JUMPNNIL && lhs.GetType() != C4V_Nil))
1900  {
1901  v = lhs;
1902  return;
1903  }
1904  n->rhs->accept(this);
1905  C4Value &rhs = v;
1906 
1907  AssertValueType(lhs, op.Type1, op.Identifier, n);
1908  AssertValueType(rhs, op.Type2, op.Identifier, n);
1909 
1910  switch (op.Code)
1911  {
1912  case AB_Pow:
1913  v.SetInt(Pow(lhs._getInt(), rhs._getInt()));
1914  break;
1915  case AB_Div:
1916  ENSURE_COND(rhs._getInt() != 0, "division by zero");
1917  ENSURE_COND(lhs._getInt() != INT32_MIN || rhs._getInt() != -1, "division overflow");
1918  v.SetInt(lhs._getInt() / rhs._getInt());
1919  break;
1920  case AB_Mul:
1921  v.SetInt(lhs._getInt() * rhs._getInt());
1922  break;
1923  case AB_Mod:
1924  ENSURE_COND(rhs._getInt() != 0, "division by zero");
1925  ENSURE_COND(lhs._getInt() != INT32_MIN || rhs._getInt() != -1, "division overflow");
1926  v.SetInt(lhs._getInt() / rhs._getInt());
1927  break;
1928 #define INT_BINOP(code, op) case code: v.SetInt(lhs._getInt() op rhs._getInt()); break
1929  INT_BINOP(AB_Sum, +);
1930  INT_BINOP(AB_Sub, -);
1931  INT_BINOP(AB_LeftShift, << );
1932  INT_BINOP(AB_RightShift, >> );
1933  INT_BINOP(AB_BitAnd, &);
1934  INT_BINOP(AB_BitXOr, ^);
1935  INT_BINOP(AB_BitOr, | );
1936 #undef INT_BINOP
1937 #define BOOL_BINOP(code, op) case code: v.SetBool(lhs._getInt() op rhs._getInt()); break
1938  BOOL_BINOP(AB_LessThan, <);
1942 #undef BOOL_BINOP
1943  case AB_Equal:
1944  v.SetBool(lhs.IsIdenticalTo(rhs));
1945  break;
1946  case AB_NotEqual:
1947  v.SetBool(!lhs.IsIdenticalTo(rhs));
1948  break;
1949  case AB_JUMPAND:
1950  case AB_JUMPOR:
1951  case AB_JUMPNNIL:
1952  // If we hit this, then the short-circuit above failed
1953  v = rhs;
1954  break;
1955  default:
1956  assert(!"ConstexprEvaluator: Unexpected binary operator");
1957  throw Error(host, host, n, nullptr, "internal error: binary operator not found in operator table");
1958  break;
1959  }
1960 }
const C4ScriptOpDef C4ScriptOpMap[]
Definition: C4AulParse.cpp:274
#define BOOL_BINOP(code, op)
void SetBool(bool b)
Definition: C4Value.h:137
#define ENSURE_COND(cond, failmsg)
C4V_Type GetType() const
Definition: C4Value.h:161
bool IsIdenticalTo(const C4Value &cmp) const
Definition: C4Value.h:149
void SetInt(int32_t i)
Definition: C4Value.h:136
int32_t _getInt() const
Definition: C4Value.h:122
int Pow(int base, int exponent)
Definition: Standard.cpp:69
#define INT_BINOP(code, op)
Here is the call graph for this function:

◆ visit() [28/32]

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

Reimplemented from aul::AstVisitor.

Definition at line 1962 of file C4AulCompiler.cpp.

1963 {
1964  nonconst(n, "updating assignment used in a non-const fashion");
1965 }

◆ visit() [29/32]

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

Reimplemented from aul::AstVisitor.

Definition at line 1967 of file C4AulCompiler.cpp.

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

1968 {
1969  n->object->accept(this);
1970  C4Value obj = v;
1971  n->index->accept(this);
1972  C4Value &index = v;
1973 
1974  if (obj.CheckConversion(C4V_Array))
1975  {
1976  ENSURE_COND(index.CheckConversion(C4V_Int), FormatString("array access: index of type %s, but expected int", index.GetTypeName()).getData());
1977  v = obj.getArray()->GetItem(index.getInt());
1978  }
1979  else if (obj.CheckConversion(C4V_PropList))
1980  {
1981  ENSURE_COND(index.CheckConversion(C4V_String), FormatString("proplist access: index of type %s, but expected string", index.GetTypeName()).getData());
1982  if (!obj.getPropList()->GetPropertyByS(index.getStr(), &v))
1983  v.Set0();
1984  }
1985  else
1986  {
1987  ENSURE_COND(false, FormatString("can't access %s as array or proplist", obj.GetTypeName()).getData());
1988  }
1989 }
const char * GetTypeName() const
Definition: C4Value.h:164
int32_t getInt() const
Definition: C4Value.h:112
C4String * getStr() const
Definition: C4Value.h:117
C4PropList * getPropList() const
Definition: C4Value.h:116
C4ValueArray * getArray() const
Definition: C4Value.h:118
#define ENSURE_COND(cond, failmsg)
const C4Value & GetItem(int32_t iElem) const
Definition: C4ValueArray.h:38
ALWAYS_INLINE bool CheckConversion(C4V_Type vtToType) const
Definition: C4Value.h:189
virtual bool GetPropertyByS(const C4String *k, C4Value *pResult) const
Definition: C4PropList.cpp:757
StdStrBuf FormatString(const char *szFmt,...)
Definition: StdBuf.cpp:270
Here is the call graph for this function:

◆ visit() [30/32]

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

Reimplemented from aul::AstVisitor.

Definition at line 1991 of file C4AulCompiler.cpp.

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

1992 {
1993  n->object->accept(this);
1994  C4Value obj = v;
1995  n->start->accept(this);
1996  C4Value start = v;
1997  n->end->accept(this);
1998  C4Value &end = v;
1999 
2000  ENSURE_COND(obj.CheckConversion(C4V_Array), FormatString("array slice: can't access %s as an array", obj.GetTypeName()).getData());
2001  ENSURE_COND(start.CheckConversion(C4V_Int), FormatString("array slice: start index of type %s, int expected", start.GetTypeName()).getData());
2002  ENSURE_COND(end.CheckConversion(C4V_Int), FormatString("array slice: end index of type %s, int expected", end.GetTypeName()).getData());
2003 
2004  v.SetArray(obj.getArray()->GetSlice(start.getInt(), end.getInt()));
2005 }
const char * GetTypeName() const
Definition: C4Value.h:164
int32_t getInt() const
Definition: C4Value.h:112
C4ValueArray * getArray() const
Definition: C4Value.h:118
#define ENSURE_COND(cond, failmsg)
void SetArray(C4ValueArray *Array)
Definition: C4Value.h:139
ALWAYS_INLINE bool CheckConversion(C4V_Type vtToType) const
Definition: C4Value.h:189
C4ValueArray * GetSlice(int32_t startIndex, int32_t endIndex)
StdStrBuf FormatString(const char *szFmt,...)
Definition: StdBuf.cpp:270
Here is the call graph for this function:

◆ visit() [31/32]

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

Reimplemented from aul::AstVisitor.

Definition at line 2007 of file C4AulCompiler.cpp.

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

◆ visit() [32/32]

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

Reimplemented from aul::AstVisitor.

Definition at line 2013 of file C4AulCompiler.cpp.

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

2014 {
2015  // Function expressions can only occur inside static proplists.
2016  ENSURE_COND(proplist_magic.active, "internal error: function expression outside of static proplist");
2017 
2018  C4AulScriptFunc *sfunc = nullptr;
2019  bool first_pass = true;
2020 
2021  if (auto func = proplist_magic.parent->GetFunc(proplist_magic.key.c_str()))
2022  {
2023  sfunc = func->SFunc();
2024  first_pass = false;
2025  }
2026  else
2027  {
2028  sfunc = new C4AulScriptFunc(proplist_magic.parent, host, proplist_magic.key.c_str(), n->loc);
2029  }
2030 
2031  ENSURE_COND(sfunc != nullptr, "internal error: function expression target resolved to non-function value");
2032 
2033  if (first_pass)
2034  {
2035  for (const auto &param : n->params)
2036  {
2037  sfunc->AddPar(param.name.c_str());
2038  }
2039  if (n->has_unnamed_params)
2040  sfunc->ParCount = C4AUL_MAX_Par;
2041 
2042  PreparseAstVisitor preparser(host, host, sfunc);
2043  preparser.visit(n->body.get());
2044  }
2045  else
2046  {
2047  CodegenAstVisitor cg(sfunc);
2048  cg.EmitFunctionCode(n);
2049  }
2050 
2051  v.SetFunction(sfunc);
2052 }
#define ENSURE_COND(cond, failmsg)
std::vector< Parameter > params
Definition: C4AulAST.h:377
std::unique_ptr< Block > body
Definition: C4AulAST.h:379
#define C4AUL_MAX_Par
Definition: C4AulFunc.h:26
bool has_unnamed_params
Definition: C4AulAST.h:378
C4AulScriptFunc * SFunc() override
void AddPar(const char *Idtf, C4V_Type type=C4V_Any)
void SetFunction(C4AulFunc *Fn)
Definition: C4Value.h:140
const char * loc
Definition: C4AulAST.h:123
Here is the call graph for this function:

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