The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2175 lines
83KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. The code included in this file is provided under the terms of the ISC license
  8. http://www.isc.org/downloads/software-support-policy/isc-license. Permission
  9. To use, copy, modify, and/or distribute this software for any purpose with or
  10. without fee is hereby granted provided that the above copyright notice and
  11. this permission notice appear in all copies.
  12. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  13. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  14. DISCLAIMED.
  15. ==============================================================================
  16. */
  17. namespace littlefoot
  18. {
  19. using namespace juce;
  20. /**
  21. This class compiles littlefoot source code into a littlefoot::Program object
  22. which can be executed by a littlefoot::Runner.
  23. */
  24. struct Compiler
  25. {
  26. Compiler() = default;
  27. /** Gives the compiler a zero-terminated list of native function prototypes to
  28. use when parsing function calls.
  29. */
  30. void addNativeFunctions (const char* const* functionPrototypes)
  31. {
  32. for (; *functionPrototypes != nullptr; ++functionPrototypes)
  33. nativeFunctions.add (NativeFunction (*functionPrototypes, nullptr));
  34. }
  35. /** Tells the compiler to use the list of native function prototypes from
  36. this littlefoot::Runner object.
  37. */
  38. template <typename RunnerType>
  39. void addNativeFunctions (const RunnerType& runner)
  40. {
  41. for (int i = 0; i < runner.getNumNativeFunctions(); ++i)
  42. nativeFunctions.add (runner.getNativeFunction (i));
  43. }
  44. /** Compiles a littlefoot program.
  45. If there's an error, this returns it, otherwise the compiled bytecode is
  46. placed in the compiledObjectCode member.
  47. */
  48. Result compile (const String& sourceCode, uint32 defaultHeapSize)
  49. {
  50. try
  51. {
  52. SyntaxTreeBuilder stb (sourceCode, nativeFunctions, defaultHeapSize);
  53. stb.compile();
  54. stb.simplify();
  55. compiledObjectCode.clear();
  56. CodeGenerator codeGen (compiledObjectCode, stb);
  57. codeGen.generateCode (stb.blockBeingParsed, stb.heapSizeRequired);
  58. return Result::ok();
  59. }
  60. catch (String error)
  61. {
  62. return Result::fail (error);
  63. }
  64. }
  65. /** After a successful compilation, this returns the finished Program. */
  66. Program getCompiledProgram() const noexcept
  67. {
  68. return Program (compiledObjectCode.begin(), (uint32) compiledObjectCode.size());
  69. }
  70. /** After a successful call to compile(), this contains the bytecode generated.
  71. A littlefoot::Program object can be created directly from this array.
  72. */
  73. Array<uint8> compiledObjectCode;
  74. private:
  75. struct Statement;
  76. struct Expression;
  77. struct BlockStatement;
  78. struct Function;
  79. struct AllocatedObject { virtual ~AllocatedObject() noexcept {} };
  80. using StatementPtr = Statement*;
  81. using ExpPtr = Expression*;
  82. using BlockPtr = BlockStatement*;
  83. using TokenType = const char*;
  84. #define LITTLEFOOT_KEYWORDS(X) \
  85. X(if_, "if") X(else_, "else") X(do_, "do") \
  86. X(while_, "while") X(for_, "for") X(break_, "break") X(continue_, "continue") \
  87. X(void_, "void") X(int_, "int") X(float_, "float") X(bool_, "bool") \
  88. X(return_, "return") X(true_, "true") X(false_, "false") X(const_, "const")
  89. #define LITTLEFOOT_OPERATORS(X) \
  90. X(semicolon, ";") X(dot, ".") X(comma, ",") X(hash, "#") \
  91. X(openParen, "(") X(closeParen, ")") X(openBrace, "{") X(closeBrace, "}") \
  92. X(openBracket, "[") X(closeBracket, "]") X(colon, ":") X(question, "?") \
  93. X(equals, "==") X(assign, "=") X(notEquals, "!=") X(logicalNot, "!") \
  94. X(plusEquals, "+=") X(plusplus, "++") X(plus, "+") \
  95. X(minusEquals, "-=") X(minusminus, "--") X(minus, "-") \
  96. X(timesEquals, "*=") X(times, "*") X(divideEquals, "/=") X(divide, "/") \
  97. X(moduloEquals, "%=") X(modulo, "%") X(xorEquals, "^=") X(bitwiseXor, "^") X(bitwiseNot, "~") \
  98. X(andEquals, "&=") X(logicalAnd, "&&") X(bitwiseAnd, "&") \
  99. X(orEquals, "|=") X(logicalOr, "||") X(bitwiseOr, "|") \
  100. X(leftShiftEquals, "<<=") X(lessThanOrEqual, "<=") X(leftShift, "<<") X(lessThan, "<") \
  101. X(rightShiftUnsigned, ">>>") X(rightShiftEquals, ">>=") X(rightShift, ">>") X(greaterThanOrEqual, ">=") X(greaterThan, ">")
  102. struct Token
  103. {
  104. #define DECLARE_LITTLEFOOT_TOKEN(name, str) static constexpr const char* name = str;
  105. LITTLEFOOT_KEYWORDS (DECLARE_LITTLEFOOT_TOKEN)
  106. LITTLEFOOT_OPERATORS (DECLARE_LITTLEFOOT_TOKEN)
  107. DECLARE_LITTLEFOOT_TOKEN (eof, "$eof")
  108. DECLARE_LITTLEFOOT_TOKEN (literal, "$literal")
  109. DECLARE_LITTLEFOOT_TOKEN (identifier, "$identifier")
  110. };
  111. Array<NativeFunction> nativeFunctions;
  112. //==============================================================================
  113. struct CodeLocation
  114. {
  115. CodeLocation (const String& code) noexcept : program (code), location (program.getCharPointer()) {}
  116. CodeLocation (const CodeLocation& other) noexcept : program (other.program), location (other.location) {}
  117. [[noreturn]] void throwError (const String& message) const
  118. {
  119. int col = 1, line = 1;
  120. for (auto i = program.getCharPointer(); i < location && ! i.isEmpty(); ++i)
  121. {
  122. ++col;
  123. if (*i == '\n') { col = 1; ++line; }
  124. }
  125. throw "Line " + String (line) + ", column " + String (col) + " : " + message;
  126. }
  127. String program;
  128. String::CharPointerType location;
  129. };
  130. //==============================================================================
  131. struct TokenIterator
  132. {
  133. TokenIterator (const String& code) : location (code), p (code.getCharPointer()) { skip(); }
  134. TokenType skip()
  135. {
  136. skipWhitespaceAndComments();
  137. location.location = p;
  138. auto last = currentType;
  139. currentType = matchNextToken();
  140. return last;
  141. }
  142. void match (TokenType expected)
  143. {
  144. if (currentType != expected)
  145. throwErrorExpecting (getTokenDescription (expected));
  146. skip();
  147. }
  148. bool matchIf (TokenType expected) { if (currentType == expected) { skip(); return true; } return false; }
  149. template <typename... Args>
  150. bool matchesAny (TokenType t1, Args... others) const noexcept { return currentType == t1 || matchesAny (others...); }
  151. bool matchesAny (TokenType t1) const noexcept { return currentType == t1; }
  152. CodeLocation location;
  153. TokenType currentType;
  154. var currentValue;
  155. void throwErrorExpecting (const String& expected) { location.throwError ("Found " + getTokenDescription (currentType) + " when expecting " + expected); }
  156. private:
  157. String::CharPointerType p;
  158. static bool isIdentifierStart (const juce_wchar c) noexcept { return CharacterFunctions::isLetter (c) || c == '_'; }
  159. static bool isIdentifierBody (const juce_wchar c) noexcept { return CharacterFunctions::isLetterOrDigit (c) || c == '_'; }
  160. TokenType matchNextToken()
  161. {
  162. if (isIdentifierStart (*p))
  163. {
  164. auto end = p;
  165. while (isIdentifierBody (*++end)) {}
  166. const size_t len = (size_t) (end - p);
  167. #define LITTLEFOOT_COMPARE_KEYWORD(name, str) if (len == sizeof (str) - 1 && matchToken (Token::name, len)) return Token::name;
  168. LITTLEFOOT_KEYWORDS (LITTLEFOOT_COMPARE_KEYWORD)
  169. currentValue = String (p, end); p = end;
  170. return Token::identifier;
  171. }
  172. if (p.isDigit())
  173. {
  174. if (parseHexLiteral() || parseFloatLiteral() || parseOctalLiteral() || parseDecimalLiteral())
  175. return Token::literal;
  176. location.throwError ("Syntax error in numeric constant");
  177. }
  178. if (parseStringLiteral (*p) || (*p == '.' && parseFloatLiteral()))
  179. return Token::literal;
  180. #define LITTLEFOOT_COMPARE_OPERATOR(name, str) if (matchToken (Token::name, sizeof (str) - 1)) return Token::name;
  181. LITTLEFOOT_OPERATORS (LITTLEFOOT_COMPARE_OPERATOR)
  182. if (! p.isEmpty())
  183. location.throwError ("Unexpected character '" + String::charToString (*p) + "' in source");
  184. return Token::eof;
  185. }
  186. bool matchToken (TokenType name, const size_t len) noexcept
  187. {
  188. if (p.compareUpTo (CharPointer_ASCII (name), (int) len) != 0) return false;
  189. p += (int) len; return true;
  190. }
  191. void skipWhitespaceAndComments()
  192. {
  193. for (;;)
  194. {
  195. p = p.findEndOfWhitespace();
  196. if (*p == '/')
  197. {
  198. const juce_wchar c2 = p[1];
  199. if (c2 == '/') { p = CharacterFunctions::find (p, (juce_wchar) '\n'); continue; }
  200. if (c2 == '*')
  201. {
  202. location.location = p;
  203. p = CharacterFunctions::find (p + 2, CharPointer_ASCII ("*/"));
  204. if (p.isEmpty()) location.throwError ("Unterminated '/*' comment");
  205. p += 2; continue;
  206. }
  207. }
  208. break;
  209. }
  210. }
  211. bool parseStringLiteral (juce_wchar quoteType)
  212. {
  213. if (quoteType != '"' && quoteType != '\'')
  214. return false;
  215. auto r = JSON::parseQuotedString (p, currentValue);
  216. if (r.failed()) location.throwError (r.getErrorMessage());
  217. return true;
  218. }
  219. bool parseHexLiteral()
  220. {
  221. if (*p != '0' || (p[1] != 'x' && p[1] != 'X')) return false;
  222. auto t = ++p;
  223. auto v = CharacterFunctions::getHexDigitValue (*++t);
  224. if (v < 0) return false;
  225. for (;;)
  226. {
  227. auto digit = CharacterFunctions::getHexDigitValue (*++t);
  228. if (digit < 0) break;
  229. v = v * 16 + digit;
  230. }
  231. currentValue = v; p = t;
  232. return true;
  233. }
  234. bool parseFloatLiteral()
  235. {
  236. int numDigits = 0;
  237. auto t = p;
  238. while (t.isDigit()) { ++t; ++numDigits; }
  239. const bool hasPoint = (*t == '.');
  240. if (hasPoint)
  241. while ((++t).isDigit()) ++numDigits;
  242. if (numDigits == 0)
  243. return false;
  244. auto c = *t;
  245. const bool hasExponent = (c == 'e' || c == 'E');
  246. if (hasExponent)
  247. {
  248. c = *++t;
  249. if (c == '+' || c == '-') ++t;
  250. if (! t.isDigit()) return false;
  251. while ((++t).isDigit()) {}
  252. }
  253. if (! (hasExponent || hasPoint)) return false;
  254. currentValue = CharacterFunctions::getDoubleValue (p); p = t;
  255. return true;
  256. }
  257. bool parseOctalLiteral()
  258. {
  259. auto t = p;
  260. int64 v = *t - '0';
  261. if (v != 0) return false; // first digit of octal must be 0
  262. for (;;)
  263. {
  264. auto digit = (int) (*++t - '0');
  265. if (isPositiveAndBelow (digit, 8)) v = v * 8 + digit;
  266. else if (isPositiveAndBelow (digit, 10)) location.throwError ("Decimal digit in octal constant");
  267. else break;
  268. }
  269. currentValue = v; p = t;
  270. return true;
  271. }
  272. bool parseDecimalLiteral()
  273. {
  274. int64 v = 0;
  275. for (;; ++p)
  276. {
  277. auto digit = (int) (*p - '0');
  278. if (isPositiveAndBelow (digit, 10)) v = v * 10 + digit;
  279. else break;
  280. }
  281. currentValue = v;
  282. return true;
  283. }
  284. };
  285. //==============================================================================
  286. //==============================================================================
  287. struct SyntaxTreeBuilder : private TokenIterator
  288. {
  289. SyntaxTreeBuilder (const String& code, const Array<NativeFunction>& nativeFns, uint32 defaultHeapSize)
  290. : TokenIterator (code), nativeFunctions (nativeFns), heapSizeRequired (defaultHeapSize) {}
  291. void compile()
  292. {
  293. blockBeingParsed = allocate<BlockStatement> (location, nullptr, nullptr, false);
  294. while (currentType != Token::eof)
  295. {
  296. if (matchIf (Token::hash))
  297. {
  298. parseCompilerDirective();
  299. continue;
  300. }
  301. const bool isConstVariable = matchIf (Token::const_);
  302. if (! matchesAnyTypeOrVoid())
  303. throwErrorExpecting ("a global variable or function");
  304. auto type = tokenToType (skip());
  305. auto name = parseIdentifier();
  306. if (matchIf (Token::openParen))
  307. {
  308. if (isConstVariable)
  309. location.throwError ("Return type of a function cannot be const");
  310. parseFunctionDeclaration (type, name);
  311. continue;
  312. }
  313. if (type == Type::void_)
  314. location.throwError ("A variable type cannot be 'void'");
  315. parseGlobalVariableDeclaraion (isConstVariable, type, name);
  316. }
  317. }
  318. void simplify()
  319. {
  320. for (auto f : functions)
  321. f->block->simplify (*this);
  322. }
  323. Function* findFunction (FunctionID functionID) const noexcept
  324. {
  325. for (auto f : functions)
  326. if (f->functionID == functionID)
  327. return f;
  328. return nullptr;
  329. }
  330. NativeFunction* findNativeFunction (FunctionID functionID) const noexcept
  331. {
  332. for (auto& f : nativeFunctions)
  333. if (f.functionID == functionID)
  334. return &f;
  335. return nullptr;
  336. }
  337. //==============================================================================
  338. BlockPtr blockBeingParsed = nullptr;
  339. Array<Function*> functions;
  340. const Array<NativeFunction>& nativeFunctions;
  341. uint32 heapSizeRequired;
  342. template <typename Type, typename... Args>
  343. Type* allocate (Args... args) { auto o = new Type (args...); allAllocatedObjects.add (o); return o; }
  344. private:
  345. OwnedArray<AllocatedObject> allAllocatedObjects;
  346. //==============================================================================
  347. void parseCompilerDirective()
  348. {
  349. auto name = parseIdentifier();
  350. if (name == "heapsize")
  351. {
  352. match (Token::colon);
  353. heapSizeRequired = (((uint32) parseIntegerLiteral()) + 3) & ~3u;
  354. return;
  355. }
  356. location.throwError ("Unknown compiler directive");
  357. }
  358. void parseGlobalVariableDeclaraion (bool isConst, Type type, String name)
  359. {
  360. for (;;)
  361. {
  362. int arraySize = matchIf (Token::openBracket) ? parseIntegerLiteral() : 0;
  363. if (arraySize > 0)
  364. location.throwError ("Arrays not yet implemented!");
  365. var constantInitialiser;
  366. if (isConst)
  367. constantInitialiser = parseConstantExpressionInitialiser (type);
  368. blockBeingParsed->addVariable ({ name, type, true, isConst, constantInitialiser }, location);
  369. if (matchIf (Token::comma))
  370. {
  371. name = parseIdentifier();
  372. continue;
  373. }
  374. match (Token::semicolon);
  375. break;
  376. }
  377. }
  378. var parseConstantExpressionInitialiser (Type expectedType)
  379. {
  380. var result;
  381. match (Token::assign);
  382. auto e = parseExpression();
  383. if (auto literal = dynamic_cast<LiteralValue*> (e->simplify (*this)))
  384. {
  385. result = literal->value;
  386. if (getTypeOfVar (result) != expectedType)
  387. location.throwError ("Expected a constant expression of type " + getTypeName (expectedType));
  388. }
  389. else
  390. {
  391. location.throwError ("Expected a constant expression");
  392. }
  393. return result;
  394. }
  395. void parseFunctionDeclaration (Type returnType, const String& name)
  396. {
  397. auto f = allocate<Function>();
  398. while (matchesAnyType())
  399. {
  400. auto type = tokenToType (skip());
  401. f->arguments.add ({ parseIdentifier(), type, false, false, 0 });
  402. if (f->arguments.size() > 127)
  403. location.throwError ("Too many function arguments");
  404. if (currentType == Token::closeParen)
  405. break;
  406. match (Token::comma);
  407. }
  408. match (Token::closeParen);
  409. f->functionID = createFunctionID (name, returnType, f->getArgumentTypes());
  410. if (findFunction (f->functionID) != nullptr || findNativeFunction (f->functionID) != nullptr)
  411. location.throwError ("Duplicate function declaration");
  412. functions.add (f);
  413. f->block = parseBlock (true);
  414. f->returnType = returnType;
  415. if (! f->block->alwaysReturns())
  416. {
  417. if (returnType != Type::void_)
  418. location.throwError ("This function must return a value");
  419. f->block->statements.add (allocate<ReturnStatement> (location, f->block, nullptr));
  420. }
  421. }
  422. int parseIntegerLiteral()
  423. {
  424. auto e = parseExpression();
  425. if (auto literal = dynamic_cast<LiteralValue*> (e->simplify (*this)))
  426. {
  427. if (literal->value.isInt() || literal->value.isInt64())
  428. {
  429. auto value = static_cast<int> (literal->value);
  430. if (value > 0)
  431. return value;
  432. }
  433. }
  434. location.throwError ("Expected an integer constant");
  435. return 0;
  436. }
  437. BlockPtr parseBlock (bool isMainBlockOfFunction)
  438. {
  439. match (Token::openBrace);
  440. auto b = allocate<BlockStatement> (location, blockBeingParsed, functions.getLast(), isMainBlockOfFunction);
  441. auto lastBlock = blockBeingParsed;
  442. blockBeingParsed = b;
  443. while (! matchIf (Token::closeBrace))
  444. b->statements.add (parseStatement());
  445. blockBeingParsed = lastBlock;
  446. return b;
  447. }
  448. StatementPtr parseStatement()
  449. {
  450. if (currentType == Token::openBrace) return parseBlock (false);
  451. if (matchIf (Token::if_)) return parseIf();
  452. if (matchIf (Token::while_)) return parseDoOrWhileLoop (false);
  453. if (matchIf (Token::do_)) return parseDoOrWhileLoop (true);
  454. if (matchIf (Token::for_)) return parseForLoop();
  455. if (matchIf (Token::return_)) return parseReturn();
  456. if (matchIf (Token::break_)) return matchEndOfStatement (allocate<BreakStatement> (location, blockBeingParsed));
  457. if (matchIf (Token::continue_)) return matchEndOfStatement (allocate<ContinueStatement> (location, blockBeingParsed));
  458. if (matchIf (Token::semicolon)) return matchEndOfStatement (allocate<Statement> (location, blockBeingParsed));
  459. if (matchIf (Token::plusplus)) return matchEndOfStatement (parsePreIncDec (Token::plus));
  460. if (matchIf (Token::minusminus)) return matchEndOfStatement (parsePreIncDec (Token::minus));
  461. if (matchesAny (Token::openParen)) return matchEndOfStatement (parseFactor());
  462. if (matchIf (Token::const_)) return parseVariableDeclaration (true);
  463. if (matchesAnyType()) return parseVariableDeclaration (false);
  464. if (matchesAny (Token::identifier, Token::literal, Token::minus))
  465. return matchEndOfStatement (parseExpression());
  466. throwErrorExpecting ("a statement");
  467. return nullptr;
  468. }
  469. ExpPtr parseExpression()
  470. {
  471. auto lhs = parseLogicOperator();
  472. if (matchIf (Token::question)) return parseTernaryOperator (lhs);
  473. if (matchIf (Token::plusEquals)) return parseInPlaceOpExpression (lhs, Token::plus);
  474. if (matchIf (Token::minusEquals)) return parseInPlaceOpExpression (lhs, Token::minus);
  475. if (matchIf (Token::timesEquals)) return parseInPlaceOpExpression (lhs, Token::times);
  476. if (matchIf (Token::divideEquals)) return parseInPlaceOpExpression (lhs, Token::divide);
  477. if (matchIf (Token::moduloEquals)) return parseInPlaceOpExpression (lhs, Token::modulo);
  478. if (matchIf (Token::leftShiftEquals)) return parseInPlaceOpExpression (lhs, Token::leftShift);
  479. if (matchIf (Token::rightShiftEquals)) return parseInPlaceOpExpression (lhs, Token::rightShift);
  480. if (matchIf (Token::assign))
  481. {
  482. auto loc = location;
  483. return allocate<Assignment> (loc, blockBeingParsed, getIdentifierFromExpression (lhs), parseExpression(), false);
  484. }
  485. return lhs;
  486. }
  487. ExpPtr parseTernaryOperator (ExpPtr condition)
  488. {
  489. auto e = allocate<TernaryOp> (location, blockBeingParsed);
  490. e->condition = condition;
  491. e->trueBranch = parseExpression();
  492. match (Token::colon);
  493. e->falseBranch = parseExpression();
  494. return e;
  495. }
  496. ExpPtr parseLogicOperator()
  497. {
  498. for (auto a = parseComparator();;)
  499. {
  500. if (! matchesAny (Token::logicalAnd, Token::logicalOr, Token::bitwiseOr,
  501. Token::bitwiseAnd, Token::bitwiseXor))
  502. return a;
  503. auto loc = location;
  504. auto type = skip();
  505. a = allocate<BinaryOperator> (loc, blockBeingParsed, a, parseComparator(), type);
  506. }
  507. }
  508. ExpPtr parseComparator()
  509. {
  510. for (auto a = parseShiftOperator();;)
  511. {
  512. if (! matchesAny (Token::equals, Token::notEquals, Token::lessThan,
  513. Token::lessThanOrEqual, Token::greaterThan, Token::greaterThanOrEqual))
  514. return a;
  515. auto loc = location;
  516. auto type = skip();
  517. a = allocate<BinaryOperator> (loc, blockBeingParsed, a, parseShiftOperator(), type);
  518. }
  519. }
  520. ExpPtr parseShiftOperator()
  521. {
  522. for (auto a = parseAdditionSubtraction();;)
  523. {
  524. if (! matchesAny (Token::leftShift, Token::rightShift, Token::rightShiftUnsigned))
  525. return a;
  526. auto loc = location;
  527. auto type = skip();
  528. a = allocate<BinaryOperator> (loc, blockBeingParsed, a, parseExpression(), type);
  529. }
  530. }
  531. ExpPtr parseAdditionSubtraction()
  532. {
  533. for (auto a = parseMultiplyDivide();;)
  534. {
  535. if (! matchesAny (Token::plus, Token::minus))
  536. return a;
  537. auto loc = location;
  538. auto type = skip();
  539. a = allocate<BinaryOperator> (loc, blockBeingParsed, a, parseMultiplyDivide(), type);
  540. }
  541. }
  542. ExpPtr parseMultiplyDivide()
  543. {
  544. for (auto a = parseUnary();;)
  545. {
  546. if (! matchesAny (Token::times, Token::divide, Token::modulo))
  547. return a;
  548. auto loc = location;
  549. auto type = skip();
  550. a = allocate<BinaryOperator> (loc, blockBeingParsed, a, parseUnary(), type);
  551. }
  552. }
  553. ExpPtr parseUnary()
  554. {
  555. if (matchIf (Token::plusplus)) return parsePreIncDec (Token::plus);
  556. if (matchIf (Token::minusminus)) return parsePreIncDec (Token::minus);
  557. if (matchesAny (Token::minus, Token::logicalNot, Token::bitwiseNot))
  558. {
  559. auto loc = location;
  560. auto type = skip();
  561. return allocate<UnaryOp> (loc, blockBeingParsed, parseUnary(), type);
  562. }
  563. return parseFactor();
  564. }
  565. ExpPtr parseFactor()
  566. {
  567. if (currentType == Token::identifier) return parseSuffixes (allocate<Identifier> (location, blockBeingParsed, parseIdentifier()));
  568. if (matchIf (Token::openParen)) return parseSuffixes (matchCloseParen (parseExpression()));
  569. if (matchIf (Token::true_)) return parseSuffixes (allocate<LiteralValue> (location, blockBeingParsed, true));
  570. if (matchIf (Token::false_)) return parseSuffixes (allocate<LiteralValue> (location, blockBeingParsed, false));
  571. if (currentType == Token::literal)
  572. {
  573. auto lit = allocate<LiteralValue> (location, blockBeingParsed, currentValue);
  574. skip();
  575. return parseSuffixes (lit);
  576. }
  577. if (matchesAny (Token::int_, Token::float_, Token::bool_))
  578. return parseSuffixes (parseFunctionCall (skip()));
  579. throwErrorExpecting ("an expression");
  580. return nullptr;
  581. }
  582. ExpPtr parseSuffixes (ExpPtr input)
  583. {
  584. if (currentType == Token::openParen)
  585. {
  586. if (auto functionName = dynamic_cast<Identifier*> (input))
  587. return parseSuffixes (parseFunctionCall (functionName->name));
  588. location.throwError ("Malformed function call");
  589. return {};
  590. }
  591. if (matchIf (Token::openBracket))
  592. {
  593. auto s = allocate<ArraySubscript> (location, blockBeingParsed);
  594. s->object = input;
  595. s->index = parseExpression();
  596. match (Token::closeBracket);
  597. return parseSuffixes (s);
  598. }
  599. if (matchIf (Token::plusplus)) return parsePostIncDec (input, Token::plus);
  600. if (matchIf (Token::minusminus)) return parsePostIncDec (input, Token::minus);
  601. return input;
  602. }
  603. ExpPtr parseInPlaceOpExpression (ExpPtr lhs, TokenType opType)
  604. {
  605. auto loc = location;
  606. auto rhs = parseExpression();
  607. return allocate<Assignment> (loc, blockBeingParsed, getIdentifierFromExpression (lhs),
  608. allocate<BinaryOperator> (location, blockBeingParsed, lhs, rhs, opType), false);
  609. }
  610. ExpPtr parsePreIncDec (TokenType opType)
  611. {
  612. auto lhs = parseFactor();
  613. auto one = allocate<LiteralValue> (location, blockBeingParsed, (int) 1);
  614. return allocate<Assignment> (location, blockBeingParsed, getIdentifierFromExpression (lhs),
  615. allocate<BinaryOperator> (location, blockBeingParsed, lhs, one, opType), false);
  616. }
  617. ExpPtr parsePostIncDec (ExpPtr lhs, TokenType opType)
  618. {
  619. auto one = allocate<LiteralValue> (location, blockBeingParsed, (int) 1);
  620. return allocate<Assignment> (location, blockBeingParsed, getIdentifierFromExpression (lhs),
  621. allocate<BinaryOperator> (location, blockBeingParsed, lhs, one, opType), true);
  622. }
  623. StatementPtr parseIf()
  624. {
  625. auto s = allocate<IfStatement> (location, blockBeingParsed);
  626. match (Token::openParen);
  627. s->condition = matchCloseParen (parseExpression());
  628. s->trueBranch = parseStatement();
  629. s->falseBranch = matchIf (Token::else_) ? parseStatement() : nullptr;
  630. return s;
  631. }
  632. StatementPtr parseReturn()
  633. {
  634. auto value = matchIf (Token::semicolon) ? nullptr : parseExpression();
  635. auto returnStatement = allocate<ReturnStatement> (location, blockBeingParsed, value);
  636. matchIf (Token::semicolon);
  637. return returnStatement;
  638. }
  639. StatementPtr parseVariableDeclaration (bool isConst)
  640. {
  641. if (isConst && ! matchesAnyType())
  642. throwErrorExpecting ("a type");
  643. auto type = tokenToType (skip());
  644. for (StatementPtr result = nullptr;;)
  645. {
  646. auto name = parseIdentifier();
  647. auto loc = location;
  648. if (isConst)
  649. {
  650. auto constantValue = parseConstantExpressionInitialiser (type);
  651. blockBeingParsed->addVariable ({ name, type, false, true, constantValue }, loc);
  652. }
  653. else
  654. {
  655. blockBeingParsed->addVariable ({ name, type, false, false, {} }, loc);
  656. auto assignedValue = matchIf (Token::assign) ? parseExpression() : nullptr;
  657. if (auto literal = dynamic_cast<LiteralValue*> (assignedValue))
  658. if (static_cast<double> (literal->value) == 0)
  659. assignedValue = nullptr;
  660. if (assignedValue != nullptr || ! blockBeingParsed->isMainBlockOfFunction) // no need to assign 0 for variables in the outer scope
  661. {
  662. if (assignedValue == nullptr)
  663. assignedValue = allocate<LiteralValue> (loc, blockBeingParsed, (int) 0);
  664. auto assignment = allocate<Assignment> (loc, blockBeingParsed, name, assignedValue, false);
  665. if (result == nullptr)
  666. {
  667. result = assignment;
  668. }
  669. else
  670. {
  671. auto block = dynamic_cast<BlockPtr> (result);
  672. if (block == nullptr)
  673. {
  674. block = allocate<BlockStatement> (loc, blockBeingParsed, functions.getLast(), false);
  675. block->statements.add (result);
  676. result = block;
  677. }
  678. block->statements.add (assignment);
  679. }
  680. }
  681. }
  682. if (matchIf (Token::semicolon))
  683. return result != nullptr ? result : allocate<Statement> (location, blockBeingParsed);
  684. match (Token::comma);
  685. }
  686. }
  687. StatementPtr parseForLoop()
  688. {
  689. auto oldBlock = blockBeingParsed;
  690. auto block = allocate<BlockStatement> (location, oldBlock, functions.getLast(), false);
  691. blockBeingParsed = block;
  692. auto loopStatement = allocate<LoopStatement> (location, blockBeingParsed, false);
  693. block->statements.add (loopStatement);
  694. match (Token::openParen);
  695. loopStatement->initialiser = parseStatement();
  696. loopStatement->condition = matchIf (Token::semicolon) ? allocate<LiteralValue> (location, blockBeingParsed, true)
  697. : matchEndOfStatement (parseExpression());
  698. loopStatement->iterator = matchIf (Token::closeParen) ? allocate<Statement> (location, blockBeingParsed)
  699. : matchCloseParen (parseExpression());
  700. loopStatement->body = parseStatement();
  701. blockBeingParsed = oldBlock;
  702. return block;
  703. }
  704. StatementPtr parseDoOrWhileLoop (bool isDoLoop)
  705. {
  706. auto loopStatement = allocate<LoopStatement> (location, blockBeingParsed, isDoLoop);
  707. loopStatement->initialiser = allocate<Statement> (location, blockBeingParsed);
  708. loopStatement->iterator = allocate<Statement> (location, blockBeingParsed);
  709. if (isDoLoop)
  710. {
  711. loopStatement->body = parseBlock (false);
  712. match (Token::while_);
  713. }
  714. match (Token::openParen);
  715. loopStatement->condition = matchCloseParen (parseExpression());
  716. if (! isDoLoop)
  717. loopStatement->body = parseStatement();
  718. return loopStatement;
  719. }
  720. String parseIdentifier()
  721. {
  722. auto name = currentValue.toString();
  723. match (Token::identifier);
  724. return name;
  725. }
  726. String getIdentifierFromExpression (ExpPtr e)
  727. {
  728. if (auto i = dynamic_cast<Identifier*> (e))
  729. return i->name;
  730. location.throwError ("This operator requires an assignable variable");
  731. return {};
  732. }
  733. ExpPtr parseFunctionCall (const String& name)
  734. {
  735. auto call = allocate<FunctionCall> (location, blockBeingParsed);
  736. call->functionName = name;
  737. match (Token::openParen);
  738. while (currentType != Token::closeParen)
  739. {
  740. call->arguments.add (parseExpression());
  741. if (currentType == Token::closeParen)
  742. break;
  743. match (Token::comma);
  744. }
  745. return matchCloseParen (call);
  746. }
  747. bool matchesAnyType() const noexcept { return matchesAny (Token::int_, Token::float_, Token::bool_); }
  748. bool matchesAnyTypeOrVoid() const noexcept { return matchesAnyType() || currentType == Token::void_; }
  749. ExpPtr matchCloseParen (ExpPtr e) { match (Token::closeParen); return e; }
  750. template<typename ExpType> ExpType matchEndOfStatement (ExpType e) { match (Token::semicolon); return e; }
  751. };
  752. //==============================================================================
  753. //==============================================================================
  754. struct CodeGenerator
  755. {
  756. CodeGenerator (Array<uint8>& output, const SyntaxTreeBuilder& stb)
  757. : outputCode (output), syntaxTree (stb) {}
  758. void generateCode (BlockPtr outerBlock, uint32 heapSizeBytesRequired)
  759. {
  760. for (auto f : syntaxTree.functions)
  761. {
  762. f->address = createMarker();
  763. f->unwindAddress = createMarker();
  764. }
  765. emit ((int16) 0); // checksum
  766. emit ((int16) 0); // size
  767. emit ((int16) syntaxTree.functions.size());
  768. emit ((int16) outerBlock->variables.size());
  769. emit ((int16) heapSizeBytesRequired);
  770. for (auto f : syntaxTree.functions)
  771. emit (f->functionID, f->address);
  772. auto codeStart = outputCode.size();
  773. for (auto f : syntaxTree.functions)
  774. f->emit (*this);
  775. removeJumpsToNextInstruction (codeStart);
  776. resolveMarkers();
  777. Program::writeInt16 (outputCode.begin() + 2, (int16) outputCode.size());
  778. const Program program (outputCode.begin(), (uint32) outputCode.size());
  779. Program::writeInt16 (outputCode.begin(), (int16) program.calculateChecksum());
  780. jassert (program.checksumMatches());
  781. }
  782. //==============================================================================
  783. Array<uint8>& outputCode;
  784. const SyntaxTreeBuilder& syntaxTree;
  785. struct Marker { int index = 0; };
  786. struct MarkerAndAddress { Marker marker; int address; };
  787. int nextMarker = 0;
  788. Array<MarkerAndAddress> markersToResolve, resolvedMarkers;
  789. Marker createMarker() noexcept { Marker m; m.index = ++nextMarker; return m; }
  790. void attachMarker (Marker m) { resolvedMarkers.add ({ m, outputCode.size() }); }
  791. int getResolvedMarkerAddress (Marker marker) const
  792. {
  793. for (auto m : resolvedMarkers)
  794. if (m.marker.index == marker.index)
  795. return m.address;
  796. jassertfalse;
  797. return 0;
  798. }
  799. Marker getMarkerAtAddress (int address) const noexcept
  800. {
  801. for (auto m : markersToResolve)
  802. if (m.address == address)
  803. return m.marker;
  804. jassertfalse;
  805. return {};
  806. }
  807. void resolveMarkers()
  808. {
  809. for (auto m : markersToResolve)
  810. Program::writeInt16 (outputCode.begin() + m.address, (int16) getResolvedMarkerAddress (m.marker));
  811. }
  812. void removeCode (int address, int size)
  813. {
  814. outputCode.removeRange (address, size);
  815. for (int i = markersToResolve.size(); --i >= 0;)
  816. {
  817. auto& m = markersToResolve.getReference (i);
  818. if (m.address >= address + size)
  819. m.address -= size;
  820. else if (m.address >= address)
  821. markersToResolve.remove (i);
  822. }
  823. for (auto& m : resolvedMarkers)
  824. if (m.address >= address + size)
  825. m.address -= size;
  826. }
  827. void removeJumpsToNextInstruction (int address)
  828. {
  829. while (address < outputCode.size())
  830. {
  831. auto op = (OpCode) outputCode.getUnchecked (address);
  832. auto opSize = 1 + Program::getNumExtraBytesForOpcode (op);
  833. if (op == OpCode::jump)
  834. {
  835. auto marker = getMarkerAtAddress (address + 1);
  836. if (marker.index != 0)
  837. {
  838. if (getResolvedMarkerAddress (marker) == address + opSize)
  839. {
  840. removeCode (address, opSize);
  841. continue;
  842. }
  843. }
  844. }
  845. address += opSize;
  846. }
  847. }
  848. Marker breakTarget, continueTarget;
  849. //==============================================================================
  850. void emit (OpCode op) { emit ((int8) op); }
  851. void emit (Marker m) { markersToResolve.add ({ m, outputCode.size() }); emit ((int16) 0); }
  852. void emit (int8 value) { outputCode.add ((uint8) value); }
  853. void emit (int16 value) { uint8 d[2]; Program::writeInt16 (d, value); outputCode.insertArray (-1, d, (int) sizeof (d)); }
  854. void emit (int32 value) { uint8 d[4]; Program::writeInt32 (d, value); outputCode.insertArray (-1, d, (int) sizeof (d)); }
  855. template <typename Arg1, typename... Args>
  856. void emit (Arg1 arg1, Args... args)
  857. {
  858. emit (arg1);
  859. emit (args...);
  860. }
  861. void emitPush (const var& value)
  862. {
  863. if (value.isDouble())
  864. {
  865. const float v = value;
  866. if (v == 0) emit (OpCode::push0);
  867. else emit (OpCode::push32, Program::floatToInt (v));
  868. }
  869. else
  870. {
  871. const int v = value;
  872. if (v == 0) emit (OpCode::push0);
  873. else if (v == 1) emit (OpCode::push1);
  874. else if (v > 0 && v < 128) emit (OpCode::push8, (int8) v);
  875. else if (v > 0 && v < 32768) emit (OpCode::push16, (int16) v);
  876. else emit (OpCode::push32, (int32) v);
  877. }
  878. }
  879. void emitCast (Type source, Type dest, const CodeLocation& location)
  880. {
  881. if (dest == source) return;
  882. if (dest == Type::void_) return emit (OpCode::drop);
  883. if (source == Type::bool_ && dest == Type::int_) return;
  884. if (source == Type::int_ && dest == Type::bool_) return emit (OpCode::testNZ_int32);
  885. if ((source == Type::int_ || source == Type::bool_) && dest == Type::float_) return emit (OpCode::int32ToFloat);
  886. location.throwError ("Cannot cast from " + getTypeName (source) + " to " + getTypeName (dest));
  887. }
  888. void emitVariableRead (Type sourceType, Type requiredType, int stackDepth, int index, const CodeLocation& location)
  889. {
  890. if (index < 0)
  891. {
  892. emit (OpCode::dupFromGlobal, (int16) ((-index) - 1));
  893. }
  894. else
  895. {
  896. index += stackDepth;
  897. if (index == 0)
  898. emit (OpCode::dup);
  899. else if (index < 8)
  900. emit ((OpCode) ((int) OpCode::dupOffset_01 + index - 1));
  901. else if (index >= 128)
  902. emit (OpCode::dupOffset16, (int16) index);
  903. else
  904. emit (OpCode::dupOffset, (int8) index);
  905. }
  906. emitCast (sourceType, requiredType, location);
  907. }
  908. };
  909. //==============================================================================
  910. //==============================================================================
  911. struct Statement : public AllocatedObject
  912. {
  913. struct Visitor
  914. {
  915. virtual ~Visitor() {}
  916. virtual void operator()(StatementPtr) = 0;
  917. };
  918. Statement (const CodeLocation& l, BlockPtr parent) noexcept : location (l), parentBlock (parent) {}
  919. virtual void emit (CodeGenerator&, Type, int /*stackDepth*/) const {}
  920. virtual bool alwaysReturns() const { return false; }
  921. virtual void visitSubStatements (Visitor&) const {}
  922. virtual Statement* simplify (SyntaxTreeBuilder&) { return this; }
  923. CodeLocation location;
  924. BlockPtr parentBlock;
  925. };
  926. struct Expression : public Statement
  927. {
  928. Expression (const CodeLocation& l, BlockPtr parent) noexcept : Statement (l, parent) {}
  929. virtual Type getType (CodeGenerator&) const = 0;
  930. virtual ExpPtr simplify (SyntaxTreeBuilder&) override { return this; }
  931. };
  932. struct Variable
  933. {
  934. String name;
  935. Type type;
  936. bool isGlobal, isConst;
  937. var constantValue;
  938. };
  939. //==============================================================================
  940. struct Function : public AllocatedObject
  941. {
  942. FunctionID functionID;
  943. Type returnType;
  944. Array<Variable> arguments;
  945. BlockPtr block;
  946. CodeGenerator::Marker address, unwindAddress;
  947. void emit (CodeGenerator& cg) const
  948. {
  949. cg.attachMarker (address);
  950. const int numLocals = getNumLocals();
  951. for (int num = numLocals; num > 0;)
  952. {
  953. if (num == 1)
  954. {
  955. cg.emit (OpCode::push0);
  956. --num;
  957. }
  958. else
  959. {
  960. int numToDo = jmin (127, num);
  961. cg.emit (OpCode::pushMultiple0, (int8) numToDo);
  962. num -= numToDo;
  963. }
  964. }
  965. block->emit (cg, Type::void_, 0);
  966. cg.attachMarker (unwindAddress);
  967. const bool keepTop = returnType != Type::void_;
  968. for (int num = numLocals; num > 0;)
  969. {
  970. if (num == 1 && ! keepTop)
  971. {
  972. cg.emit (OpCode::drop);
  973. --num;
  974. }
  975. else
  976. {
  977. int numToDo = jmin (127, num);
  978. cg.emit (OpCode::dropMultiple, (int8) (keepTop ? -numToDo : numToDo));
  979. num -= numToDo;
  980. }
  981. }
  982. cg.emit (keepTop ? OpCode::retValue : OpCode::retVoid, (int8) arguments.size());
  983. }
  984. Array<Type> getArgumentTypes() const
  985. {
  986. Array<Type> argTypes;
  987. for (auto& arg : arguments)
  988. argTypes.add (arg.type);
  989. return argTypes;
  990. }
  991. int getNumLocals() const noexcept
  992. {
  993. return countMaxNumLocalVariables (block);
  994. }
  995. static int countMaxNumLocalVariables (StatementPtr s) noexcept
  996. {
  997. struct Counter : Statement::Visitor
  998. {
  999. void operator() (StatementPtr sub) override
  1000. {
  1001. num = jmax (num, countMaxNumLocalVariables (sub));
  1002. }
  1003. int num = 0;
  1004. };
  1005. Counter counter;
  1006. if (s != nullptr)
  1007. s->visitSubStatements (counter);
  1008. if (auto block = dynamic_cast<BlockPtr> (s))
  1009. counter.num += block->variables.size();
  1010. return counter.num;
  1011. }
  1012. };
  1013. //==============================================================================
  1014. struct BlockStatement : public Statement
  1015. {
  1016. BlockStatement (const CodeLocation& l, BlockPtr parent, Function* f, bool isMainFunctionBlock)
  1017. : Statement (l, parent), function (f), isMainBlockOfFunction (isMainFunctionBlock) {}
  1018. void emit (CodeGenerator& cg, Type requiredType, int stackDepth) const override
  1019. {
  1020. jassert (requiredType == Type::void_); ignoreUnused (requiredType);
  1021. jassert (function != nullptr);
  1022. for (auto s : statements)
  1023. s->emit (cg, Type::void_, stackDepth);
  1024. }
  1025. bool alwaysReturns() const override
  1026. {
  1027. return ! statements.isEmpty() && statements.getLast()->alwaysReturns();
  1028. }
  1029. void visitSubStatements (Statement::Visitor& visit) const override
  1030. {
  1031. for (auto s : statements)
  1032. visit (s);
  1033. }
  1034. Statement* simplify (SyntaxTreeBuilder& stb) override
  1035. {
  1036. for (int i = 0; i < statements.size(); ++i)
  1037. statements.set (i, statements.getReference(i)->simplify (stb));
  1038. return this;
  1039. }
  1040. // returns -ve values for globals
  1041. int getVariableDepth (const String& name, const CodeLocation& locationForError) const
  1042. {
  1043. int index = indexOf (variables, name);
  1044. if (index >= 0)
  1045. return getNumVariablesInParentBlocks() + index;
  1046. if (! isMainBlockOfFunction)
  1047. return parentBlock->getVariableDepth (name, locationForError);
  1048. for (int i = function->arguments.size(); --i >= 0;)
  1049. if (function->arguments.getReference(i).name == name)
  1050. return i + 1 + function->getNumLocals();
  1051. index = indexOf (getGlobalVariables(), name);
  1052. if (index >= 0)
  1053. return -(index + 1);
  1054. locationForError.throwError ("Unknown variable '" + name + "'");
  1055. return 0;
  1056. }
  1057. int getNumVariablesInParentBlocks() const noexcept
  1058. {
  1059. return isMainBlockOfFunction ? 0 : (parentBlock->getNumVariablesInParentBlocks()
  1060. + parentBlock->variables.size());
  1061. }
  1062. const Array<Variable>& getGlobalVariables() const noexcept { return parentBlock != nullptr ? parentBlock->getGlobalVariables() : variables; }
  1063. const Array<Variable>& getGlobalConstants() const noexcept { return parentBlock != nullptr ? parentBlock->getGlobalConstants() : constants; }
  1064. const Variable& getVariable (const String& name, const CodeLocation& locationForError) const
  1065. {
  1066. for (auto& v : constants)
  1067. if (v.name == name)
  1068. return v;
  1069. for (auto& v : variables)
  1070. if (v.name == name)
  1071. return v;
  1072. if (! isMainBlockOfFunction)
  1073. return parentBlock->getVariable (name, locationForError);
  1074. for (auto& v : function->arguments)
  1075. if (v.name == name)
  1076. return v;
  1077. for (auto& v : getGlobalConstants())
  1078. if (v.name == name)
  1079. return v;
  1080. for (auto& v : getGlobalVariables())
  1081. if (v.name == name)
  1082. return v;
  1083. locationForError.throwError ("Unknown variable '" + name + "'");
  1084. }
  1085. void addVariable (Variable v, const CodeLocation& locationForError)
  1086. {
  1087. if (indexOf (variables, v.name) >= 0 || indexOf (constants, v.name) >= 0)
  1088. locationForError.throwError ("Variable '" + v.name + "' already exists");
  1089. (v.isConst ? constants : variables).add (v);
  1090. }
  1091. static int indexOf (const Array<Variable>& vars, const String& name) noexcept
  1092. {
  1093. for (int i = 0; i < vars.size(); ++i)
  1094. if (vars.getReference(i).name == name)
  1095. return i;
  1096. return -1;
  1097. }
  1098. Function* function;
  1099. Array<StatementPtr> statements;
  1100. Array<Variable> variables, constants;
  1101. bool isMainBlockOfFunction;
  1102. };
  1103. struct IfStatement : public Statement
  1104. {
  1105. IfStatement (const CodeLocation& l, BlockPtr parent) : Statement (l, parent) {}
  1106. void emit (CodeGenerator& cg, Type requiredType, int stackDepth) const override
  1107. {
  1108. jassert (requiredType == Type::void_); ignoreUnused (requiredType);
  1109. condition->emit (cg, Type::bool_, stackDepth);
  1110. auto endOfStatement = cg.createMarker();
  1111. if (falseBranch == nullptr)
  1112. {
  1113. cg.emit (OpCode::jumpIfFalse, endOfStatement);
  1114. trueBranch->emit (cg, Type::void_, stackDepth);
  1115. }
  1116. else
  1117. {
  1118. auto elseTarget = cg.createMarker();
  1119. cg.emit (OpCode::jumpIfFalse, elseTarget);
  1120. trueBranch->emit (cg, Type::void_, stackDepth);
  1121. cg.emit (OpCode::jump, endOfStatement);
  1122. cg.attachMarker (elseTarget);
  1123. falseBranch->emit (cg, Type::void_, stackDepth);
  1124. }
  1125. cg.attachMarker (endOfStatement);
  1126. }
  1127. bool alwaysReturns() const override
  1128. {
  1129. return trueBranch->alwaysReturns() && falseBranch != nullptr && falseBranch->alwaysReturns();
  1130. }
  1131. void visitSubStatements (Statement::Visitor& visit) const override
  1132. {
  1133. visit (condition); visit (trueBranch); visit (falseBranch);
  1134. }
  1135. Statement* simplify (SyntaxTreeBuilder& stb) override
  1136. {
  1137. condition = condition->simplify (stb);
  1138. trueBranch = trueBranch->simplify (stb);
  1139. falseBranch = falseBranch != nullptr ? falseBranch->simplify (stb) : nullptr;
  1140. if (auto literal = dynamic_cast<LiteralValue*> (condition))
  1141. return literal->value ? trueBranch : (falseBranch != nullptr ? falseBranch : stb.allocate<Statement> (location, parentBlock));
  1142. return this;
  1143. }
  1144. ExpPtr condition;
  1145. StatementPtr trueBranch, falseBranch;
  1146. };
  1147. struct TernaryOp : public Expression
  1148. {
  1149. TernaryOp (const CodeLocation& l, BlockPtr parent) : Expression (l, parent) {}
  1150. void emit (CodeGenerator& cg, Type requiredType, int stackDepth) const override
  1151. {
  1152. condition->emit (cg, Type::bool_, stackDepth);
  1153. auto endOfStatement = cg.createMarker();
  1154. auto elseTarget = cg.createMarker();
  1155. cg.emit (OpCode::jumpIfFalse, elseTarget);
  1156. trueBranch->emit (cg, requiredType, stackDepth);
  1157. cg.emit (OpCode::jump, endOfStatement);
  1158. cg.attachMarker (elseTarget);
  1159. falseBranch->emit (cg, requiredType, stackDepth);
  1160. cg.attachMarker (endOfStatement);
  1161. }
  1162. Type getType (CodeGenerator& cg) const override
  1163. {
  1164. auto type = trueBranch->getType (cg);
  1165. if (type == Type::void_) location.throwError ("The ternary operator cannot take void arguments");
  1166. if (type != falseBranch->getType (cg)) location.throwError ("Expected both branches of this ternary operator to have the same type");
  1167. return type;
  1168. }
  1169. void visitSubStatements (Statement::Visitor& visit) const override
  1170. {
  1171. visit (condition); visit (trueBranch); visit (falseBranch);
  1172. }
  1173. ExpPtr simplify (SyntaxTreeBuilder& stb) override
  1174. {
  1175. condition = condition->simplify (stb);
  1176. trueBranch = trueBranch->simplify (stb);
  1177. falseBranch = falseBranch->simplify (stb);
  1178. if (auto literal = dynamic_cast<LiteralValue*> (condition))
  1179. return literal->value ? trueBranch : falseBranch;
  1180. return this;
  1181. }
  1182. ExpPtr condition, trueBranch, falseBranch;
  1183. };
  1184. struct LoopStatement : public Statement
  1185. {
  1186. LoopStatement (const CodeLocation& l, BlockPtr parent, bool isDo) noexcept : Statement (l, parent), isDoLoop (isDo) {}
  1187. void emit (CodeGenerator& cg, Type, int stackDepth) const override
  1188. {
  1189. initialiser->emit (cg, Type::void_, stackDepth);
  1190. auto loopStart = cg.createMarker();
  1191. cg.attachMarker (loopStart);
  1192. auto oldBreakTarget = cg.breakTarget;
  1193. auto oldContinueTarget = cg.continueTarget;
  1194. cg.breakTarget = cg.createMarker();
  1195. cg.continueTarget = cg.createMarker();
  1196. if (isDoLoop)
  1197. {
  1198. body->emit (cg, Type::void_, stackDepth);
  1199. cg.attachMarker (cg.continueTarget);
  1200. condition->emit (cg, Type::bool_, stackDepth);
  1201. cg.emit (OpCode::jumpIfTrue, loopStart);
  1202. }
  1203. else
  1204. {
  1205. condition->emit (cg, Type::bool_, stackDepth);
  1206. cg.emit (OpCode::jumpIfFalse, cg.breakTarget);
  1207. body->emit (cg, Type::void_, stackDepth);
  1208. cg.attachMarker (cg.continueTarget);
  1209. iterator->emit (cg, Type::void_, stackDepth);
  1210. cg.emit (OpCode::jump, loopStart);
  1211. }
  1212. cg.attachMarker (cg.breakTarget);
  1213. cg.breakTarget = oldBreakTarget;
  1214. cg.continueTarget = oldContinueTarget;
  1215. }
  1216. void visitSubStatements (Statement::Visitor& visit) const override
  1217. {
  1218. visit (condition); visit (initialiser); visit (iterator); visit (body);
  1219. }
  1220. StatementPtr initialiser, iterator, body;
  1221. ExpPtr condition;
  1222. bool isDoLoop;
  1223. };
  1224. struct ReturnStatement : public Statement
  1225. {
  1226. ReturnStatement (const CodeLocation& l, BlockPtr parent, ExpPtr v) noexcept : Statement (l, parent), returnValue (v) {}
  1227. void emit (CodeGenerator& cg, Type, int stackDepth) const override
  1228. {
  1229. if (auto fn = parentBlock->function)
  1230. {
  1231. if (returnValue != nullptr)
  1232. returnValue->emit (cg, fn->returnType, stackDepth);
  1233. else if (fn->returnType != Type::void_)
  1234. location.throwError ("Cannot return a value from a void function");
  1235. cg.emit (OpCode::jump, fn->unwindAddress);
  1236. return;
  1237. }
  1238. location.throwError ("The return statement can only be used inside a function");
  1239. }
  1240. bool alwaysReturns() const override { return true; }
  1241. void visitSubStatements (Statement::Visitor& visit) const override
  1242. {
  1243. visit (returnValue);
  1244. }
  1245. ExpPtr returnValue;
  1246. };
  1247. struct BreakStatement : public Statement
  1248. {
  1249. BreakStatement (const CodeLocation& l, BlockPtr parent) : Statement (l, parent) {}
  1250. void emit (CodeGenerator& cg, Type, int) const override
  1251. {
  1252. if (cg.breakTarget.index == 0)
  1253. location.throwError ("The break statement can only be used inside a loop");
  1254. cg.emit (OpCode::jump, cg.breakTarget);
  1255. }
  1256. };
  1257. struct ContinueStatement : public Statement
  1258. {
  1259. ContinueStatement (const CodeLocation& l, BlockPtr parent) : Statement (l, parent) {}
  1260. void emit (CodeGenerator& cg, Type, int) const override
  1261. {
  1262. if (cg.continueTarget.index == 0)
  1263. location.throwError ("The continue statement can only be used inside a loop");
  1264. cg.emit (OpCode::jump, cg.continueTarget);
  1265. }
  1266. };
  1267. struct LiteralValue : public Expression
  1268. {
  1269. LiteralValue (const CodeLocation& l, BlockPtr parent, const var& v) noexcept : Expression (l, parent), value (v) {}
  1270. void emit (CodeGenerator& cg, Type requiredType, int) const override
  1271. {
  1272. if (requiredType != Type::void_)
  1273. {
  1274. auto type = getType (cg);
  1275. if (type != requiredType && value != var ((int) 0))
  1276. {
  1277. if (type == Type::int_ && requiredType == Type::bool_) return cg.emitPush (static_cast<bool> (value));
  1278. if (type == Type::int_ && requiredType == Type::float_) return cg.emitPush (static_cast<float> (value));
  1279. if (! (type == Type::bool_ && requiredType == Type::int_))
  1280. location.throwError ("Cannot cast from " + getTypeName (type) + " to " + getTypeName (requiredType));
  1281. }
  1282. cg.emitPush (value);
  1283. }
  1284. }
  1285. Type getType (CodeGenerator&) const override
  1286. {
  1287. auto t = getTypeOfVar (value);
  1288. if (t == Type::void_)
  1289. location.throwError ("Unsupported literal type");
  1290. return t;
  1291. }
  1292. var value;
  1293. };
  1294. struct Identifier : public Expression
  1295. {
  1296. Identifier (const CodeLocation& l, BlockPtr parent, const String& n) noexcept : Expression (l, parent), name (n) {}
  1297. void emit (CodeGenerator& cg, Type requiredType, int stackDepth) const override
  1298. {
  1299. cg.emitVariableRead (getType (cg), requiredType, stackDepth,
  1300. parentBlock->getVariableDepth (name, location), location);
  1301. }
  1302. Type getType (CodeGenerator&) const override
  1303. {
  1304. return parentBlock->getVariable (name, location).type;
  1305. }
  1306. ExpPtr simplify (SyntaxTreeBuilder& stb) override
  1307. {
  1308. auto& v = parentBlock->getVariable (name, location);
  1309. if (v.isConst)
  1310. return stb.allocate<LiteralValue> (location, parentBlock, v.constantValue);
  1311. return this;
  1312. }
  1313. String name;
  1314. };
  1315. struct UnaryOp : public Expression
  1316. {
  1317. UnaryOp (const CodeLocation& l, BlockPtr parent, ExpPtr a, TokenType op) noexcept
  1318. : Expression (l, parent), source (a), operation (op) {}
  1319. ExpPtr source;
  1320. TokenType operation;
  1321. void emit (CodeGenerator& cg, Type requiredType, int stackDepth) const override
  1322. {
  1323. auto sourceType = source->getType (cg);
  1324. if (operation == Token::minus)
  1325. {
  1326. cg.emitPush ((int) 0);
  1327. source->emit (cg, sourceType, stackDepth + 1);
  1328. cg.emit (sourceType == Type::float_ ? OpCode::sub_float : OpCode::sub_int32);
  1329. cg.emitCast (sourceType, requiredType, location);
  1330. }
  1331. else
  1332. {
  1333. if (sourceType == Type::float_)
  1334. location.throwError ("Cannot perform this operation on a float");
  1335. if (operation == Token::logicalNot)
  1336. {
  1337. source->emit (cg, sourceType, stackDepth);
  1338. cg.emit (OpCode::logicalNot);
  1339. cg.emitCast (Type::bool_, requiredType, location);
  1340. }
  1341. else if (operation == Token::bitwiseNot)
  1342. {
  1343. source->emit (cg, Type::int_, stackDepth);
  1344. cg.emit (OpCode::bitwiseNot);
  1345. cg.emitCast (Type::int_, requiredType, location);
  1346. }
  1347. }
  1348. }
  1349. Type getType (CodeGenerator& cg) const override
  1350. {
  1351. if (operation == Token::minus) return source->getType (cg);
  1352. if (operation == Token::logicalNot) return Type::bool_;
  1353. return Type::int_;
  1354. }
  1355. void visitSubStatements (Statement::Visitor& visit) const override
  1356. {
  1357. visit (source);
  1358. }
  1359. ExpPtr simplify (SyntaxTreeBuilder& stb) override
  1360. {
  1361. source = source->simplify (stb);
  1362. if (auto literal = dynamic_cast<LiteralValue*> (source))
  1363. {
  1364. auto type = getTypeOfVar (literal->value);
  1365. if (type == Type::int_ && operation == Token::minus) { literal->value = -static_cast<int> (literal->value); return literal; }
  1366. if (type == Type::int_ && operation == Token::bitwiseNot) { literal->value = ~static_cast<int> (literal->value); return literal; }
  1367. if (type == Type::int_ && operation == Token::logicalNot) { literal->value = ! static_cast<int> (literal->value); return literal; }
  1368. if (type == Type::bool_ && operation == Token::logicalNot) { literal->value = ! static_cast<bool> (literal->value); return literal; }
  1369. if (type == Type::float_ && operation == Token::minus) { literal->value = -static_cast<double> (literal->value); return literal; }
  1370. }
  1371. return this;
  1372. }
  1373. };
  1374. struct BinaryOperator : public Expression
  1375. {
  1376. BinaryOperator (const CodeLocation& l, BlockPtr parent, ExpPtr a, ExpPtr b, TokenType op) noexcept
  1377. : Expression (l, parent), lhs (a), rhs (b), operation (op) {}
  1378. ExpPtr lhs, rhs;
  1379. TokenType operation;
  1380. void emit (CodeGenerator& cg, Type requiredType, int stackDepth) const override
  1381. {
  1382. auto typeA = lhs->getType (cg);
  1383. auto typeB = rhs->getType (cg);
  1384. if (typeA == Type::float_ || typeB == Type::float_)
  1385. {
  1386. lhs->emit (cg, Type::float_, stackDepth);
  1387. rhs->emit (cg, Type::float_, stackDepth + 1);
  1388. if (operation == Token::plus) return emitOpAndCast (cg, requiredType, OpCode::add_float);
  1389. if (operation == Token::minus) return emitOpAndCast (cg, requiredType, OpCode::sub_float);
  1390. if (operation == Token::times) return emitOpAndCast (cg, requiredType, OpCode::mul_float);
  1391. if (operation == Token::divide) return emitOpAndCast (cg, requiredType, OpCode::div_float);
  1392. cg.emit (OpCode::sub_float);
  1393. if (operation == Token::equals) return emitOpAndCast (cg, requiredType, OpCode::testZE_float);
  1394. if (operation == Token::notEquals) return emitOpAndCast (cg, requiredType, OpCode::testNZ_float);
  1395. if (operation == Token::lessThan) return emitOpAndCast (cg, requiredType, OpCode::testLT_float);
  1396. if (operation == Token::lessThanOrEqual) return emitOpAndCast (cg, requiredType, OpCode::testLE_float);
  1397. if (operation == Token::greaterThan) return emitOpAndCast (cg, requiredType, OpCode::testGT_float);
  1398. if (operation == Token::greaterThanOrEqual) return emitOpAndCast (cg, requiredType, OpCode::testGE_float);
  1399. location.throwError ("The operator " + getTokenDescription (operation) + " cannot take floating point arguments");
  1400. }
  1401. auto type = (typeA == Type::bool_ || typeB == Type::bool_) ? Type::bool_
  1402. : Type::int_;
  1403. lhs->emit (cg, type, stackDepth);
  1404. rhs->emit (cg, type, stackDepth + 1);
  1405. if (operation == Token::plus) return emitOpAndCast (cg, requiredType, OpCode::add_int32);
  1406. if (operation == Token::minus) return emitOpAndCast (cg, requiredType, OpCode::sub_int32);
  1407. if (operation == Token::times) return emitOpAndCast (cg, requiredType, OpCode::mul_int32);
  1408. if (operation == Token::divide) return emitOpAndCast (cg, requiredType, OpCode::div_int32);
  1409. if (operation == Token::modulo) return emitOpAndCast (cg, requiredType, OpCode::mod_int32);
  1410. if (operation == Token::logicalOr) return emitOpAndCast (cg, requiredType, OpCode::logicalOr);
  1411. if (operation == Token::logicalAnd) return emitOpAndCast (cg, requiredType, OpCode::logicalAnd);
  1412. if (operation == Token::bitwiseOr) return emitOpAndCast (cg, requiredType, OpCode::bitwiseOr);
  1413. if (operation == Token::bitwiseAnd) return emitOpAndCast (cg, requiredType, OpCode::bitwiseAnd);
  1414. if (operation == Token::bitwiseXor) return emitOpAndCast (cg, requiredType, OpCode::bitwiseXor);
  1415. if (operation == Token::leftShift) return emitOpAndCast (cg, requiredType, OpCode::bitShiftLeft);
  1416. if (operation == Token::rightShift) return emitOpAndCast (cg, requiredType, OpCode::bitShiftRight);
  1417. cg.emit (OpCode::sub_int32);
  1418. if (operation == Token::equals) return emitOpAndCast (cg, requiredType, OpCode::testZE_int32);
  1419. if (operation == Token::notEquals) return emitOpAndCast (cg, requiredType, OpCode::testNZ_int32);
  1420. if (operation == Token::lessThan) return emitOpAndCast (cg, requiredType, OpCode::testLT_int32);
  1421. if (operation == Token::lessThanOrEqual) return emitOpAndCast (cg, requiredType, OpCode::testLE_int32);
  1422. if (operation == Token::greaterThan) return emitOpAndCast (cg, requiredType, OpCode::testGT_int32);
  1423. if (operation == Token::greaterThanOrEqual) return emitOpAndCast (cg, requiredType, OpCode::testGE_int32);
  1424. location.throwError ("Unsupported operator");
  1425. jassertfalse;
  1426. }
  1427. void emitOpAndCast (CodeGenerator& cg, Type requiredType, OpCode op) const
  1428. {
  1429. cg.emit (op);
  1430. cg.emitCast (getType (cg), requiredType, location);
  1431. }
  1432. Type getResultType (Type typeA, Type typeB) const noexcept
  1433. {
  1434. if (operation == Token::logicalOr || operation == Token::logicalAnd
  1435. || operation == Token::equals || operation == Token::notEquals
  1436. || operation == Token::lessThan || operation == Token::lessThanOrEqual
  1437. || operation == Token::greaterThan || operation == Token::greaterThanOrEqual)
  1438. return Type::bool_;
  1439. if (operation == Token::plus || operation == Token::minus
  1440. || operation == Token::times || operation == Token::divide)
  1441. {
  1442. if (typeA == Type::float_ || typeB == Type::float_)
  1443. return Type::float_;
  1444. }
  1445. return Type::int_;
  1446. }
  1447. Type getType (CodeGenerator& cg) const override
  1448. {
  1449. return getResultType (lhs->getType (cg), rhs->getType (cg));
  1450. }
  1451. void visitSubStatements (Statement::Visitor& visit) const override
  1452. {
  1453. visit (lhs); visit (rhs);
  1454. }
  1455. ExpPtr simplifyFloat (double a, double b, LiteralValue* literal)
  1456. {
  1457. if (operation == Token::plus) { literal->value = a + b; return literal; }
  1458. if (operation == Token::minus) { literal->value = a - b; return literal; }
  1459. if (operation == Token::times) { literal->value = a * b; return literal; }
  1460. if (operation == Token::divide) { literal->value = a / b; return literal; }
  1461. if (operation == Token::equals) { literal->value = a == b; return literal; }
  1462. if (operation == Token::notEquals) { literal->value = a != b; return literal; }
  1463. if (operation == Token::lessThan) { literal->value = a < b; return literal; }
  1464. if (operation == Token::lessThanOrEqual) { literal->value = a <= b; return literal; }
  1465. if (operation == Token::greaterThan) { literal->value = a > b; return literal; }
  1466. if (operation == Token::greaterThanOrEqual) { literal->value = a >= b; return literal; }
  1467. return this;
  1468. }
  1469. ExpPtr simplifyBool (bool a, bool b, LiteralValue* literal)
  1470. {
  1471. if (operation == Token::logicalOr) { literal->value = a || b; return literal; }
  1472. if (operation == Token::logicalAnd) { literal->value = a && b; return literal; }
  1473. return this;
  1474. }
  1475. ExpPtr simplifyInt (int a, int b, LiteralValue* literal)
  1476. {
  1477. if (operation == Token::plus) { literal->value = a + b; return literal; }
  1478. if (operation == Token::minus) { literal->value = a - b; return literal; }
  1479. if (operation == Token::times) { literal->value = a * b; return literal; }
  1480. if (operation == Token::divide) { literal->value = a / b; return literal; }
  1481. if (operation == Token::equals) { literal->value = a == b; return literal; }
  1482. if (operation == Token::notEquals) { literal->value = a != b; return literal; }
  1483. if (operation == Token::lessThan) { literal->value = a < b; return literal; }
  1484. if (operation == Token::lessThanOrEqual) { literal->value = a <= b; return literal; }
  1485. if (operation == Token::greaterThan) { literal->value = a > b; return literal; }
  1486. if (operation == Token::greaterThanOrEqual) { literal->value = a >= b; return literal; }
  1487. if (operation == Token::modulo) { literal->value = a % b; return literal; }
  1488. if (operation == Token::logicalOr) { literal->value = a || b; return literal; }
  1489. if (operation == Token::logicalAnd) { literal->value = a && b; return literal; }
  1490. if (operation == Token::bitwiseOr) { literal->value = a | b; return literal; }
  1491. if (operation == Token::bitwiseAnd) { literal->value = a & b; return literal; }
  1492. if (operation == Token::bitwiseXor) { literal->value = a ^ b; return literal; }
  1493. if (operation == Token::leftShift) { literal->value = a << b; return literal; }
  1494. if (operation == Token::rightShift) { literal->value = a >> b; return literal; }
  1495. return this;
  1496. }
  1497. ExpPtr simplify (SyntaxTreeBuilder& stb) override
  1498. {
  1499. lhs = lhs->simplify (stb);
  1500. rhs = rhs->simplify (stb);
  1501. if (auto literal1 = dynamic_cast<LiteralValue*> (lhs))
  1502. {
  1503. if (auto literal2 = dynamic_cast<LiteralValue*> (rhs))
  1504. {
  1505. auto resultType = getResultType (getTypeOfVar (literal1->value),
  1506. getTypeOfVar (literal2->value));
  1507. if (resultType == Type::bool_) return simplifyBool (literal1->value, literal2->value, literal1);
  1508. if (resultType == Type::int_) return simplifyInt (literal1->value, literal2->value, literal1);
  1509. if (resultType == Type::float_) return simplifyFloat (literal1->value, literal2->value, literal1);
  1510. }
  1511. }
  1512. return this;
  1513. }
  1514. };
  1515. struct Assignment : public Expression
  1516. {
  1517. Assignment (const CodeLocation& l, BlockPtr parent, const String& dest, ExpPtr source, bool isPost) noexcept
  1518. : Expression (l, parent), target (dest), newValue (source), isPostAssignment (isPost) {}
  1519. void emit (CodeGenerator& cg, Type requiredType, int stackDepth) const override
  1520. {
  1521. auto variableType = getType (cg);
  1522. if (isPostAssignment && requiredType != Type::void_)
  1523. {
  1524. cg.emitVariableRead (variableType, requiredType, stackDepth,
  1525. parentBlock->getVariableDepth (target, location), location);
  1526. ++stackDepth;
  1527. requiredType = Type::void_;
  1528. }
  1529. newValue->emit (cg, variableType, stackDepth);
  1530. auto index = parentBlock->getVariableDepth (target, location);
  1531. if (requiredType != Type::void_)
  1532. {
  1533. cg.emit (OpCode::dup);
  1534. ++stackDepth;
  1535. }
  1536. if (index < 0)
  1537. {
  1538. cg.emit (OpCode::dropToGlobal, (int16) ((-index) - 1));
  1539. }
  1540. else
  1541. {
  1542. index += stackDepth;
  1543. if (index >= 128)
  1544. cg.emit (OpCode::dropToStack16, (int16) index);
  1545. else
  1546. cg.emit (OpCode::dropToStack, (int8) index);
  1547. }
  1548. if (requiredType != Type::void_)
  1549. cg.emitCast (variableType, requiredType, location);
  1550. }
  1551. Type getType (CodeGenerator&) const override
  1552. {
  1553. return parentBlock->getVariable (target, location).type;
  1554. }
  1555. void visitSubStatements (Statement::Visitor& visit) const override
  1556. {
  1557. visit (newValue);
  1558. }
  1559. ExpPtr simplify (SyntaxTreeBuilder& stb) override
  1560. {
  1561. newValue = newValue->simplify (stb);
  1562. return this;
  1563. }
  1564. String target;
  1565. ExpPtr newValue;
  1566. bool isPostAssignment;
  1567. };
  1568. struct FunctionCall : public Expression
  1569. {
  1570. FunctionCall (const CodeLocation& l, BlockPtr parent) noexcept : Expression (l, parent) {}
  1571. void emit (CodeGenerator& cg, Type requiredType, int stackDepth) const override
  1572. {
  1573. if (functionName == Token::int_) return emitCast (cg, Type::int_, stackDepth);
  1574. if (functionName == Token::float_) return emitCast (cg, Type::float_, stackDepth);
  1575. if (functionName == Token::bool_) return emitCast (cg, Type::bool_, stackDepth);
  1576. auto functionID = getFunctionID (cg);
  1577. if (auto fn = cg.syntaxTree.findFunction (functionID))
  1578. {
  1579. emitArgs (cg, fn->getArgumentTypes(), stackDepth);
  1580. cg.emit (OpCode::call, fn->address);
  1581. cg.emitCast (fn->returnType, requiredType, location);
  1582. return;
  1583. }
  1584. if (auto nativeFn = cg.syntaxTree.findNativeFunction (functionID))
  1585. {
  1586. emitArgs (cg, getArgTypesFromFunctionName (nativeFn->nameAndArguments), stackDepth);
  1587. cg.emit (OpCode::callNative, nativeFn->functionID);
  1588. cg.emitCast (nativeFn->returnType, requiredType, location);
  1589. return;
  1590. }
  1591. if (auto b = findBuiltInFunction (functionID))
  1592. {
  1593. emitArgs (cg, getArgTypesFromFunctionName (b->name), stackDepth);
  1594. cg.emit (b->op);
  1595. cg.emitCast (b->returnType, requiredType, location);
  1596. return;
  1597. }
  1598. throwCannotFindFunctionError (cg, requiredType);
  1599. }
  1600. Type getType (CodeGenerator& cg) const override
  1601. {
  1602. if (arguments.size() == 1)
  1603. {
  1604. if (functionName == Token::float_) return Type::float_;
  1605. if (functionName == Token::int_) return Type::int_;
  1606. if (functionName == Token::bool_) return Type::bool_;
  1607. }
  1608. auto functionID = getFunctionID (cg);
  1609. if (auto fn = cg.syntaxTree.findFunction (functionID))
  1610. return fn->returnType;
  1611. if (auto nativeFn = cg.syntaxTree.findNativeFunction (functionID))
  1612. return nativeFn->returnType;
  1613. if (auto b = findBuiltInFunction (functionID))
  1614. return b->returnType;
  1615. throwCannotFindFunctionError (cg, Type::void_);
  1616. return {};
  1617. }
  1618. struct BuiltInFunction { OpCode op; Type returnType; const char* name; };
  1619. const BuiltInFunction* findBuiltInFunction (FunctionID functionID) const noexcept
  1620. {
  1621. static constexpr const BuiltInFunction builtIns[] =
  1622. {
  1623. { OpCode::getHeapByte, Type::int_, "getHeapByte/ii" },
  1624. { OpCode::getHeapInt, Type::int_, "getHeapInt/ii" },
  1625. { OpCode::getHeapBits, Type::int_, "getHeapBits/iii" },
  1626. { OpCode::setHeapByte, Type::void_, "setHeapByte/vii" },
  1627. { OpCode::setHeapInt, Type::void_, "setHeapInt/vii" }
  1628. };
  1629. for (auto& b : builtIns)
  1630. if (functionID == NativeFunction::createID (b.name))
  1631. return &b;
  1632. return nullptr;
  1633. }
  1634. void emitArgs (CodeGenerator& cg, const Array<Type>& argTypes, int stackDepth) const
  1635. {
  1636. for (int i = arguments.size(); --i >= 0;)
  1637. {
  1638. auto argType = argTypes[i];
  1639. auto argValue = arguments.getUnchecked(i);
  1640. if (argValue->getType (cg) != argType)
  1641. location.throwError ("Argument " + String (i + 1) + " requires an expression of type " + getTypeName (argType));
  1642. argValue->emit (cg, argType, stackDepth++);
  1643. }
  1644. }
  1645. void emitCast (CodeGenerator& cg, Type destType, int stackDepth) const
  1646. {
  1647. auto* arg = arguments.getReference (0);
  1648. const auto sourceType = arg->getType (cg);
  1649. arg->emit (cg, sourceType, stackDepth);
  1650. const bool sourceIsFloat = (sourceType == Type::float_);
  1651. const bool destIsFloat = (destType == Type::float_);
  1652. if (sourceIsFloat != destIsFloat)
  1653. cg.emit (destIsFloat ? OpCode::int32ToFloat : OpCode::floatToInt32);
  1654. }
  1655. FunctionID getFunctionID (CodeGenerator& cg) const
  1656. {
  1657. Array<Type> argTypes;
  1658. for (auto arg : arguments)
  1659. argTypes.add (arg->getType (cg));
  1660. return createFunctionID (functionName, Type::void_, argTypes); // NB: the ID ignores the return type so void is OK
  1661. }
  1662. void throwCannotFindFunctionError (CodeGenerator& cg, Type returnType) const
  1663. {
  1664. StringArray args;
  1665. for (auto arg : arguments)
  1666. args.add (getTypeName (arg->getType (cg)));
  1667. auto desc = getTypeName (returnType) + " " + functionName
  1668. + "(" + args.joinIntoString (", ") + ")";
  1669. location.throwError ("Cannot find matching function: " + desc.quoted());
  1670. }
  1671. void visitSubStatements (Statement::Visitor& visit) const override
  1672. {
  1673. for (auto& arg : arguments)
  1674. visit (arg);
  1675. }
  1676. ExpPtr simplify (SyntaxTreeBuilder& stb) override
  1677. {
  1678. for (auto& arg : arguments)
  1679. arg = arg->simplify (stb);
  1680. return this;
  1681. }
  1682. String functionName;
  1683. Array<ExpPtr> arguments;
  1684. };
  1685. struct ArraySubscript : public Expression
  1686. {
  1687. ArraySubscript (const CodeLocation& l, BlockPtr parent) noexcept : Expression (l, parent) {}
  1688. void emit (CodeGenerator&, Type /*requiredType*/, int /*stackDepth*/) const override
  1689. {
  1690. location.throwError ("Arrays are not implemented yet!");
  1691. }
  1692. void visitSubStatements (Statement::Visitor& visit) const override
  1693. {
  1694. visit (object); visit (index);
  1695. }
  1696. ExpPtr simplify (SyntaxTreeBuilder& stb) override
  1697. {
  1698. object = object->simplify (stb);
  1699. index = index->simplify (stb);
  1700. return this;
  1701. }
  1702. Type getType (CodeGenerator& cg) const override
  1703. {
  1704. return object->getType (cg);
  1705. }
  1706. ExpPtr object, index;
  1707. };
  1708. //==============================================================================
  1709. static Array<Type> getArgTypesFromFunctionName (const char* nameAndTypes)
  1710. {
  1711. Array<Type> types;
  1712. auto args = String (nameAndTypes).fromFirstOccurrenceOf ("/", false, false).substring (1);
  1713. for (int i = 0; i < args.length(); ++i)
  1714. types.add (static_cast<Type> (args[i]));
  1715. return types;
  1716. }
  1717. static FunctionID createFunctionID (String name, Type returnType, const Array<Type>& types)
  1718. {
  1719. name << "/" << (char) returnType;
  1720. for (auto t : types)
  1721. name << (char) t;
  1722. return NativeFunction::createID (name.toRawUTF8());
  1723. }
  1724. static String getTokenDescription (TokenType t) { return t[0] == '$' ? String (t + 1) : ("'" + String (t) + "'"); }
  1725. static String getTypeName (Type t) noexcept
  1726. {
  1727. if (t == Type::int_) return "int";
  1728. if (t == Type::bool_) return "bool";
  1729. if (t == Type::float_) return "float";
  1730. return "void";
  1731. }
  1732. static Type tokenToType (TokenType t) noexcept
  1733. {
  1734. if (t == Token::int_) return Type::int_;
  1735. if (t == Token::bool_) return Type::bool_;
  1736. if (t == Token::float_) return Type::float_;
  1737. return Type::void_;
  1738. }
  1739. static Type getTypeOfVar (const var& v) noexcept
  1740. {
  1741. if (v.isInt() || v.isInt64()) return Type::int_;
  1742. if (v.isDouble()) return Type::float_;
  1743. if (v.isBool()) return Type::bool_;
  1744. return Type::void_;
  1745. }
  1746. };
  1747. }