OpenClonk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
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 303 of file C4AulCompiler.cpp.

Member Typedef Documentation

Definition at line 317 of file C4AulCompiler.cpp.

Member Enumeration Documentation

Enumerator
IgnoreUnset 
SuppressErrors 

Definition at line 306 of file C4AulCompiler.cpp.

307  {
308  // If this flag is set, ConstexprEvaluator will assume unset values
309  // are nil. If it is not set, evaluation of unset values will send an
310  // ExpressionNotConstant to the error handler.
311  IgnoreUnset = 1<<0,
312  // If this flag is set, ConstexprEvaluator will not send exceptions to
313  // the error handler (so it doesn't report them twice: once from the
314  // preparsing step, then again from the compile step).
315  SuppressErrors = 1<<1
316  };

Member Function Documentation

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

Definition at line 1706 of file C4AulCompiler.cpp.

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

1707 {
1708  ConstexprEvaluator ce(host);
1709  ce.ignore_unset_values = (flags & IgnoreUnset) == IgnoreUnset;
1710  try
1711  {
1712  e->accept(&ce);
1713  return ce.v;
1714  }
1715  catch (C4AulParseError &e)
1716  {
1717  if ((flags & SuppressErrors) == 0)
1718  host->Engine->ErrorHandler->OnError(e.what());
1719  return C4VNull;
1720  }
1721 }
C4AulScriptEngine * Engine
Definition: C4ScriptHost.h:76
const char * what() const noexceptoverride
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:

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

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

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

1724 {
1725  ConstexprEvaluator ce(host);
1726  ce.proplist_magic = ConstexprEvaluator::ProplistMagic{ true, parent, parent_key };
1727  ce.ignore_unset_values = (flags & IgnoreUnset) == IgnoreUnset;
1728  try
1729  {
1730  e->accept(&ce);
1731  return ce.v;
1732  }
1733  catch (C4AulParseError &e)
1734  {
1735  if ((flags & SuppressErrors) == 0)
1736  host->Engine->ErrorHandler->OnError(e.what());
1737  return C4VNull;
1738  }
1739 }
C4AulScriptEngine * Engine
Definition: C4ScriptHost.h:76
const char * what() const noexceptoverride
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:

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

Reimplemented in C4AulCompiler::CodegenAstVisitor.

Definition at line 63 of file C4AulAST.h.

63 {}
virtual void aul::AstVisitor::visit ( const ::aul::ast::ParExpr )
inlinevirtualinherited
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 {}
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 {}
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 {}
virtual void aul::AstVisitor::visit ( const ::aul::ast::RangeLoop )
inlinevirtualinherited
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 {}
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 {}
virtual void aul::AstVisitor::visit ( const ::aul::ast::Break )
inlinevirtualinherited

Reimplemented in C4AulCompiler::CodegenAstVisitor.

Definition at line 85 of file C4AulAST.h.

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

Reimplemented in C4AulCompiler::CodegenAstVisitor.

Definition at line 86 of file C4AulAST.h.

86 {}
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 {}
virtual void aul::AstVisitor::visit ( const ::aul::ast::VarDecl )
inlinevirtualinherited
virtual void aul::AstVisitor::visit ( const ::aul::ast::FunctionDecl )
inlinevirtualinherited
virtual void aul::AstVisitor::visit ( const ::aul::ast::IncludePragma )
inlinevirtualinherited

Reimplemented in C4AulCompiler::PreparseAstVisitor.

Definition at line 91 of file C4AulAST.h.

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

Reimplemented in C4AulCompiler::PreparseAstVisitor.

Definition at line 92 of file C4AulAST.h.

92 {}
virtual void aul::AstVisitor::visit ( const ::aul::ast::Script )
inlinevirtualinherited
template<class T >
void aul::AstVisitor::visit ( const T *  )
deleteinherited
void C4AulCompiler::ConstexprEvaluator::visit ( const ::aul::ast::StringLit n)
overridevirtual

Reimplemented from aul::AstVisitor.

Definition at line 1741 of file C4AulCompiler.cpp.

References C4VString().

1741 { 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:

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

Reimplemented from aul::AstVisitor.

Definition at line 1743 of file C4AulCompiler.cpp.

References C4VInt().

1743 { 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:

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

Reimplemented from aul::AstVisitor.

Definition at line 1745 of file C4AulCompiler.cpp.

References C4VBool().

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

Here is the call graph for this function:

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

Reimplemented from aul::AstVisitor.

Definition at line 1747 of file C4AulCompiler.cpp.

References a, and C4VArray().

1748 {
1749  auto a = std::make_unique<C4ValueArray>(n->values.size());
1750  for (size_t i = 0; i < n->values.size(); ++i)
1751  {
1752  n->values[i]->accept(this);
1753  a->SetItem(i, v);
1754  }
1755  v = C4VArray(a.release());
1756 }
#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:

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

Reimplemented from aul::AstVisitor.

Definition at line 1758 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.

1759 {
1760  std::unique_ptr<C4PropList> new_proplist;
1761  C4PropList *p = nullptr;
1762 
1763  bool first_pass = true;
1764 
1765  if (proplist_magic.active)
1766  {
1767  // Check if there's already a proplist available
1768  C4String *key = ::Strings.RegString(proplist_magic.key.c_str());
1769  C4Value old;
1770  if (proplist_magic.parent)
1771  {
1772  proplist_magic.parent->GetPropertyByS(key, &old);
1773  }
1774  else
1775  {
1776  // If proplist_magic.parent is nullptr, we're handling a global constant.
1777  host->Engine->GetGlobalConstant(key->GetCStr(), &old);
1778  }
1779  if (old.getPropList())
1780  {
1781  p = old.getPropList();
1782  first_pass = false;
1783  }
1784  else
1785  {
1786  p = C4PropList::NewStatic(nullptr, proplist_magic.parent, key);
1787  new_proplist.reset(p);
1788  }
1789  }
1790  else
1791  {
1792  p = C4PropList::New();
1793  new_proplist.reset(p);
1794  }
1795 
1796  // Since the values may be functions that refer to other values in the
1797  // proplist, pre-populate the new proplist with dummy values until the
1798  // real ones are set
1799  if (first_pass)
1800  {
1801  for (const auto &kv : n->values)
1802  {
1803  p->SetPropertyByS(::Strings.RegString(kv.first.c_str()), C4VNull);
1804  }
1805  }
1806 
1807  auto saved_magic = std::move(proplist_magic);
1808  for (const auto &kv : n->values)
1809  {
1810  proplist_magic = ProplistMagic { saved_magic.active, p->IsStatic(), kv.first };
1811  kv.second->accept(this);
1812  p->SetPropertyByS(::Strings.RegString(kv.first.c_str()), v);
1813  }
1814  proplist_magic = std::move(saved_magic);
1815  v = C4VPropList(p);
1816  new_proplist.release();
1817 }
const char * GetCStr() const
Definition: C4StringTable.h:49
C4String * RegString(StdStrBuf String)
bool GetGlobalConstant(const char *szName, C4Value *pTargetValue)
virtual class C4PropListStatic * IsStatic()
Definition: C4PropList.h:85
C4AulScriptEngine * Engine
Definition: C4ScriptHost.h:76
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:952
std::vector< std::pair< std::string, ExprPtr > > values
Definition: C4AulAST.h:187
static C4PropList * New(C4PropList *prototype=nullptr)
Definition: C4PropList.cpp:64
C4PropList * getPropList() const
Definition: C4Value.h:116

Here is the call graph for this function:

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

Reimplemented from aul::AstVisitor.

Definition at line 1819 of file C4AulCompiler.cpp.

References C4VNull.

1819 { v = C4VNull; }
const C4Value C4VNull
Definition: C4Value.cpp:30
void C4AulCompiler::ConstexprEvaluator::visit ( const ::aul::ast::ThisLit n)
overridevirtual

Reimplemented from aul::AstVisitor.

Definition at line 1821 of file C4AulCompiler.cpp.

1821 { nonconst(n); }
void C4AulCompiler::ConstexprEvaluator::visit ( const ::aul::ast::VarExpr n)
overridevirtual

Reimplemented from aul::AstVisitor.

Definition at line 1823 of file C4AulCompiler.cpp.

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

1824 {
1825  const char *cname = n->identifier.c_str();
1826  C4String *interned = ::Strings.FindString(cname);
1827  if (interned && host->GetPropList()->GetPropertyByS(interned, &v))
1828  return;
1829  if (host->Engine->GetGlobalConstant(cname, &v))
1830  return;
1831 
1832  if (ignore_unset_values)
1833  {
1834  v = C4VNull;
1835  return;
1836  }
1837 
1838  nonconst(n);
1839 }
C4String * FindString(const char *strString) const
bool GetGlobalConstant(const char *szName, C4Value *pTargetValue)
C4AulScriptEngine * Engine
Definition: C4ScriptHost.h:76
virtual C4PropListStatic * GetPropList()
Definition: C4ScriptHost.h:50
C4StringTable Strings
Definition: C4Globals.cpp:42
virtual bool GetPropertyByS(const C4String *k, C4Value *pResult) const
Definition: C4PropList.cpp:757
const C4Value C4VNull
Definition: C4Value.cpp:30
std::string identifier
Definition: C4AulAST.h:205

Here is the call graph for this function:

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

Reimplemented from aul::AstVisitor.

Definition at line 1841 of file C4AulCompiler.cpp.

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

1842 {
1843  n->operand->accept(this);
1844  assert(n->op > 0);
1845  const auto &op = C4ScriptOpMap[n->op];
1846  if (op.Changer)
1847  nonconst(n);
1848  AssertValueType(v, op.Type1, op.Identifier, n);
1849  switch (op.Code)
1850  {
1851  case AB_BitNot:
1852  v.SetInt(~v._getInt());
1853  break;
1854  case AB_Not:
1855  v.SetBool(!v.getBool());
1856  break;
1857  case AB_Neg:
1858  v.SetInt(-v._getInt());
1859  break;
1860  default:
1861  assert(!"ConstexprEvaluator: Unexpected unary operator");
1862  throw Error(host, host, n, nullptr, "internal error: unary operator not found in operator table");
1863  }
1864 }
const C4ScriptOpDef C4ScriptOpMap[]
Definition: C4AulParse.cpp:261
void SetBool(bool b)
Definition: C4Value.h:137
bool getBool() const
Definition: C4Value.h:113
int32_t _getInt() const
Definition: C4Value.h:122
void SetInt(int32_t i)
Definition: C4Value.h:136

Here is the call graph for this function:

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

Reimplemented from aul::AstVisitor.

Definition at line 1866 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().

1867 {
1868  assert(n->op > 0);
1869  const auto &op = C4ScriptOpMap[n->op];
1870  if (op.Changer)
1871  nonconst(n);
1872 
1873  n->lhs->accept(this);
1874  C4Value lhs = v;
1875  // Evaluate the short-circuiting operators here
1876  if ((op.Code == AB_JUMPAND && !lhs) || (op.Code == AB_JUMPOR && lhs) || (op.Code == AB_JUMPNNIL && lhs.GetType() != C4V_Nil))
1877  {
1878  v = lhs;
1879  return;
1880  }
1881  n->rhs->accept(this);
1882  C4Value &rhs = v;
1883 
1884  AssertValueType(lhs, op.Type1, op.Identifier, n);
1885  AssertValueType(rhs, op.Type2, op.Identifier, n);
1886 
1887  switch (op.Code)
1888  {
1889  case AB_Pow:
1890  v.SetInt(Pow(lhs._getInt(), rhs._getInt()));
1891  break;
1892  case AB_Div:
1893  ENSURE_COND(rhs._getInt() != 0, "division by zero");
1894  ENSURE_COND(lhs._getInt() != INT32_MIN || rhs._getInt() != -1, "division overflow");
1895  v.SetInt(lhs._getInt() / rhs._getInt());
1896  break;
1897  case AB_Mul:
1898  v.SetInt(lhs._getInt() * rhs._getInt());
1899  break;
1900  case AB_Mod:
1901  ENSURE_COND(rhs._getInt() != 0, "division by zero");
1902  ENSURE_COND(lhs._getInt() != INT32_MIN || rhs._getInt() != -1, "division overflow");
1903  v.SetInt(lhs._getInt() / rhs._getInt());
1904  break;
1905 #define INT_BINOP(code, op) case code: v.SetInt(lhs._getInt() op rhs._getInt()); break
1906  INT_BINOP(AB_Sum, +);
1907  INT_BINOP(AB_Sub, -);
1908  INT_BINOP(AB_LeftShift, << );
1909  INT_BINOP(AB_RightShift, >> );
1910  INT_BINOP(AB_BitAnd, &);
1911  INT_BINOP(AB_BitXOr, ^);
1912  INT_BINOP(AB_BitOr, | );
1913 #undef INT_BINOP
1914 #define BOOL_BINOP(code, op) case code: v.SetBool(lhs._getInt() op rhs._getInt()); break
1915  BOOL_BINOP(AB_LessThan, <);
1919 #undef BOOL_BINOP
1920  case AB_Equal:
1921  v.SetBool(lhs.IsIdenticalTo(rhs));
1922  break;
1923  case AB_NotEqual:
1924  v.SetBool(!lhs.IsIdenticalTo(rhs));
1925  break;
1926  case AB_JUMPAND:
1927  case AB_JUMPOR:
1928  case AB_JUMPNNIL:
1929  // If we hit this, then the short-circuit above failed
1930  v = rhs;
1931  break;
1932  default:
1933  assert(!"ConstexprEvaluator: Unexpected binary operator");
1934  throw Error(host, host, n, nullptr, "internal error: binary operator not found in operator table");
1935  break;
1936  }
1937 }
const C4ScriptOpDef C4ScriptOpMap[]
Definition: C4AulParse.cpp:261
#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
int32_t _getInt() const
Definition: C4Value.h:122
void SetInt(int32_t i)
Definition: C4Value.h:136
int Pow(int base, int exponent)
Definition: Standard.cpp:69
bool IsIdenticalTo(const C4Value &cmp) const
Definition: C4Value.h:149
#define INT_BINOP(code, op)

Here is the call graph for this function:

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

Reimplemented from aul::AstVisitor.

Definition at line 1939 of file C4AulCompiler.cpp.

1940 {
1941  nonconst(n);
1942 }
void C4AulCompiler::ConstexprEvaluator::visit ( const ::aul::ast::SubscriptExpr n)
overridevirtual

Reimplemented from aul::AstVisitor.

Definition at line 1944 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().

1945 {
1946  n->object->accept(this);
1947  C4Value obj = v;
1948  n->index->accept(this);
1949  C4Value &index = v;
1950 
1951  if (obj.CheckConversion(C4V_Array))
1952  {
1953  ENSURE_COND(index.CheckConversion(C4V_Int), FormatString("array access: index of type %s, but expected int", index.GetTypeName()).getData());
1954  v = obj.getArray()->GetItem(index.getInt());
1955  }
1956  else if (obj.CheckConversion(C4V_PropList))
1957  {
1958  ENSURE_COND(index.CheckConversion(C4V_String), FormatString("proplist access: index of type %s, but expected string", index.GetTypeName()).getData());
1959  if (!obj.getPropList()->GetPropertyByS(index.getStr(), &v))
1960  v.Set0();
1961  }
1962  else
1963  {
1964  ENSURE_COND(false, FormatString("can't access %s as array or proplist", obj.GetTypeName()).getData());
1965  }
1966 }
C4String * getStr() const
Definition: C4Value.h:117
#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
const char * GetTypeName() const
Definition: C4Value.h:164
virtual bool GetPropertyByS(const C4String *k, C4Value *pResult) const
Definition: C4PropList.cpp:757
C4ValueArray * getArray() const
Definition: C4Value.h:118
int32_t getInt() const
Definition: C4Value.h:112
StdStrBuf FormatString(const char *szFmt,...)
Definition: StdBuf.cpp:270
C4PropList * getPropList() const
Definition: C4Value.h:116

Here is the call graph for this function:

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

Reimplemented from aul::AstVisitor.

Definition at line 1968 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().

1969 {
1970  n->object->accept(this);
1971  C4Value obj = v;
1972  n->start->accept(this);
1973  C4Value start = v;
1974  n->end->accept(this);
1975  C4Value &end = v;
1976 
1977  ENSURE_COND(obj.CheckConversion(C4V_Array), FormatString("array slice: can't access %s as an array", obj.GetTypeName()).getData());
1978  ENSURE_COND(start.CheckConversion(C4V_Int), FormatString("array slice: start index of type %s, int expected", start.GetTypeName()).getData());
1979  ENSURE_COND(end.CheckConversion(C4V_Int), FormatString("array slice: end index of type %s, int expected", end.GetTypeName()).getData());
1980 
1981  v.SetArray(obj.getArray()->GetSlice(start.getInt(), end.getInt()));
1982 }
#define ENSURE_COND(cond, failmsg)
ALWAYS_INLINE bool CheckConversion(C4V_Type vtToType) const
Definition: C4Value.h:189
const char * GetTypeName() const
Definition: C4Value.h:164
void SetArray(C4ValueArray *Array)
Definition: C4Value.h:139
C4ValueArray * getArray() const
Definition: C4Value.h:118
int32_t getInt() const
Definition: C4Value.h:112
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:

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

Reimplemented from aul::AstVisitor.

Definition at line 1984 of file C4AulCompiler.cpp.

1985 {
1986  // TODO: allow side-effect-free calls here
1987  nonconst(n);
1988 }
void C4AulCompiler::ConstexprEvaluator::visit ( const ::aul::ast::FunctionExpr n)
overridevirtual

Reimplemented from aul::AstVisitor.

Definition at line 1990 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().

1991 {
1992  // Function expressions can only occur inside static proplists.
1993  ENSURE_COND(proplist_magic.active, "internal error: function expression outside of static proplist");
1994 
1995  C4AulScriptFunc *sfunc = nullptr;
1996  bool first_pass = true;
1997 
1998  if (auto func = proplist_magic.parent->GetFunc(proplist_magic.key.c_str()))
1999  {
2000  sfunc = func->SFunc();
2001  first_pass = false;
2002  }
2003  else
2004  {
2005  sfunc = new C4AulScriptFunc(proplist_magic.parent, host, proplist_magic.key.c_str(), n->loc);
2006  }
2007 
2008  ENSURE_COND(sfunc != nullptr, "internal error: function expression target resolved to non-function value");
2009 
2010  if (first_pass)
2011  {
2012  for (const auto &param : n->params)
2013  {
2014  sfunc->AddPar(param.name.c_str());
2015  }
2016  if (n->has_unnamed_params)
2017  sfunc->ParCount = C4AUL_MAX_Par;
2018 
2019  PreparseAstVisitor preparser(host, host, sfunc);
2020  preparser.visit(n->body.get());
2021  }
2022  else
2023  {
2024  CodegenAstVisitor cg(sfunc);
2025  cg.EmitFunctionCode(n);
2026  }
2027 
2028  v.SetFunction(sfunc);
2029 }
#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: