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.

2199 lines
84KB

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