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

virtual void visit (const ::aul::ast::StringLit *n) override
 
virtual void visit (const ::aul::ast::IntLit *n) override
 
virtual void visit (const ::aul::ast::BoolLit *n) override
 
virtual void visit (const ::aul::ast::ArrayLit *n) override
 
virtual void visit (const ::aul::ast::ProplistLit *n) override
 
virtual void visit (const ::aul::ast::NilLit *) override
 
virtual void visit (const ::aul::ast::ThisLit *n) override
 
virtual void visit (const ::aul::ast::VarExpr *n) override
 
virtual void visit (const ::aul::ast::UnOpExpr *n) override
 
virtual void visit (const ::aul::ast::BinOpExpr *n) override
 
virtual void visit (const ::aul::ast::AssignmentExpr *n) override
 
virtual void visit (const ::aul::ast::SubscriptExpr *n) override
 
virtual void visit (const ::aul::ast::SliceExpr *n) override
 
virtual void visit (const ::aul::ast::CallExpr *n) override
 
virtual 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 304 of file C4AulCompiler.cpp.

Member Typedef Documentation

Definition at line 318 of file C4AulCompiler.cpp.

Member Enumeration Documentation

Enumerator
IgnoreUnset 
SuppressErrors 

Definition at line 307 of file C4AulCompiler.cpp.

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

Member Function Documentation

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

Definition at line 1707 of file C4AulCompiler.cpp.

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

1708 {
1709  ConstexprEvaluator ce(host);
1710  ce.ignore_unset_values = (flags & IgnoreUnset) == IgnoreUnset;
1711  try
1712  {
1713  e->accept(&ce);
1714  return ce.v;
1715  }
1716  catch (C4AulParseError &e)
1717  {
1718  if ((flags & SuppressErrors) == 0)
1719  host->Engine->ErrorHandler->OnError(e.what());
1720  return C4VNull;
1721  }
1722 }
C4AulScriptEngine * Engine
Definition: C4ScriptHost.h:76
virtual const char * what() const noexcept
virtual void OnError(const char *msg)=0
const C4Value C4VNull
Definition: C4Value.cpp:32
virtual void accept(::aul::AstVisitor *) const =0
C4AulErrorHandler * ErrorHandler
Definition: C4Aul.h:130

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

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

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

1725 {
1726  ConstexprEvaluator ce(host);
1727  ce.proplist_magic = ConstexprEvaluator::ProplistMagic{ true, parent, parent_key };
1728  ce.ignore_unset_values = (flags & IgnoreUnset) == IgnoreUnset;
1729  try
1730  {
1731  e->accept(&ce);
1732  return ce.v;
1733  }
1734  catch (C4AulParseError &e)
1735  {
1736  if ((flags & SuppressErrors) == 0)
1737  host->Engine->ErrorHandler->OnError(e.what());
1738  return C4VNull;
1739  }
1740 }
C4AulScriptEngine * Engine
Definition: C4ScriptHost.h:76
virtual const char * what() const noexcept
virtual void OnError(const char *msg)=0
const C4Value C4VNull
Definition: C4Value.cpp:32
virtual void accept(::aul::AstVisitor *) const =0
C4AulErrorHandler * ErrorHandler
Definition: C4Aul.h:130

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 68 of file C4AulAST.h.

68 {}
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 84 of file C4AulAST.h.

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

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

Definition at line 85 of file C4AulAST.h.

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

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

Definition at line 86 of file C4AulAST.h.

86 {}
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 88 of file C4AulAST.h.

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

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

Definition at line 89 of file C4AulAST.h.

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

Reimplemented in C4AulCompiler::CodegenAstVisitor.

Definition at line 90 of file C4AulAST.h.

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

Reimplemented in C4AulCompiler::CodegenAstVisitor.

Definition at line 91 of file C4AulAST.h.

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

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

Definition at line 92 of file C4AulAST.h.

92 {}
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 96 of file C4AulAST.h.

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

Reimplemented in C4AulCompiler::PreparseAstVisitor.

Definition at line 97 of file C4AulAST.h.

97 {}
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 1742 of file C4AulCompiler.cpp.

References C4VString().

1742 { v = C4VString(n->value.c_str()); }
std::string value
Definition: C4AulAST.h:162
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 1744 of file C4AulCompiler.cpp.

References C4VInt().

1744 { v = C4VInt(n->value); }
C4Value C4VInt(int32_t i)
Definition: C4Value.h:242
uint32_t value
Definition: C4AulAST.h:170

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

References C4VBool().

1746 { 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 1748 of file C4AulCompiler.cpp.

References a, and C4VArray().

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

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

References C4VNull.

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

Reimplemented from aul::AstVisitor.

Definition at line 1822 of file C4AulCompiler.cpp.

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

Reimplemented from aul::AstVisitor.

Definition at line 1824 of file C4AulCompiler.cpp.

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

1825 {
1826  const char *cname = n->identifier.c_str();
1827  C4String *interned = ::Strings.FindString(cname);
1828  if (interned && host->GetPropList()->GetPropertyByS(interned, &v))
1829  return;
1830  if (host->Engine->GetGlobalConstant(cname, &v))
1831  return;
1832 
1833  if (ignore_unset_values)
1834  {
1835  v = C4VNull;
1836  return;
1837  }
1838 
1839  nonconst(n);
1840 }
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:734
const C4Value C4VNull
Definition: C4Value.cpp:32
std::string identifier
Definition: C4AulAST.h:210

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

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

1843 {
1844  n->operand->accept(this);
1845  assert(n->op > 0);
1846  const auto &op = C4ScriptOpMap[n->op];
1847  if (op.Changer)
1848  nonconst(n);
1849  AssertValueType(v, op.Type1, op.Identifier, n);
1850  switch (op.Code)
1851  {
1852  case AB_BitNot:
1853  v.SetInt(~v._getInt());
1854  break;
1855  case AB_Not:
1856  v.SetBool(!v.getBool());
1857  break;
1858  case AB_Neg:
1859  v.SetInt(-v._getInt());
1860  break;
1861  default:
1862  assert(!"ConstexprEvaluator: Unexpected unary operator");
1863  throw Error(host, host, n, nullptr, "internal error: unary operator not found in operator table");
1864  }
1865 }
const C4ScriptOpDef C4ScriptOpMap[]
Definition: C4AulParse.cpp:270
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 1867 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().

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

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

Reimplemented from aul::AstVisitor.

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

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

1970 {
1971  n->object->accept(this);
1972  C4Value obj = v;
1973  n->start->accept(this);
1974  C4Value start = v;
1975  n->end->accept(this);
1976  C4Value &end = v;
1977 
1978  ENSURE_COND(obj.CheckConversion(C4V_Array), FormatString("array slice: can't access %s as an array", obj.GetTypeName()).getData());
1979  ENSURE_COND(start.CheckConversion(C4V_Int), FormatString("array slice: start index of type %s, int expected", start.GetTypeName()).getData());
1980  ENSURE_COND(end.CheckConversion(C4V_Int), FormatString("array slice: end index of type %s, int expected", end.GetTypeName()).getData());
1981 
1982  v.SetArray(obj.getArray()->GetSlice(start.getInt(), end.getInt()));
1983 }
#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:277

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

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

Reimplemented from aul::AstVisitor.

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

1992 {
1993  // Function expressions can only occur inside static proplists.
1994  ENSURE_COND(proplist_magic.active, "internal error: function expression outside of static proplist");
1995 
1996  C4AulScriptFunc *sfunc = nullptr;
1997  bool first_pass = true;
1998 
1999  if (auto func = proplist_magic.parent->GetFunc(proplist_magic.key.c_str()))
2000  {
2001  sfunc = func->SFunc();
2002  first_pass = false;
2003  }
2004  else
2005  {
2006  sfunc = new C4AulScriptFunc(proplist_magic.parent, host, proplist_magic.key.c_str(), n->loc);
2007  }
2008 
2009  ENSURE_COND(sfunc != nullptr, "internal error: function expression target resolved to non-function value");
2010 
2011  if (first_pass)
2012  {
2013  for (const auto &param : n->params)
2014  {
2015  sfunc->AddPar(param.name.c_str());
2016  }
2017  if (n->has_unnamed_params)
2018  sfunc->ParCount = C4AUL_MAX_Par;
2019 
2020  PreparseAstVisitor preparser(host, host, sfunc);
2021  preparser.visit(n->body.get());
2022  }
2023  else
2024  {
2025  CodegenAstVisitor cg(sfunc);
2026  cg.EmitFunctionCode(n);
2027  }
2028 
2029  v.SetFunction(sfunc);
2030 }
#define ENSURE_COND(cond, failmsg)
std::vector< Parameter > params
Definition: C4AulAST.h:382
std::unique_ptr< Block > body
Definition: C4AulAST.h:384
#define C4AUL_MAX_Par
Definition: C4AulFunc.h:26
C4AulScriptFunc * SFunc()
bool has_unnamed_params
Definition: C4AulAST.h:383
void AddPar(const char *Idtf, C4V_Type type=C4V_Any)
void SetFunction(C4AulFunc *Fn)
Definition: C4Value.h:140
const char * loc
Definition: C4AulAST.h:128

Here is the call graph for this function:


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