Skip to content

Primi 0.4

Latest
Compare
Choose a tag to compare
@smuuf smuuf released this 06 Apr 00:08
· 250 commits to master since this release
  • New: If statements now support else clause.
  • New: String literals now support proper escaping (BC break).
    • You can now "escape an escape sequence" (e.g a \n newline) using a double backslash (e.g. \\n) to avoid escaping. (BC break)
      • Previously, string literal "\\n" would result in a string of new-line preceeded by a backslash \ symbol. Now "\\n" results in literal \n.
    • Also, escape sequences were previously expanded when instantiating String value objects - even, for example, when it was the result of concatenating two strings "\" and "n", which would then result in new string equal to "\n", which would be then expanded to a literal newline, which is just wrong approach.
    • With the new approach escape sequences are expanded only at the time of the evaluating string literals in Primi source code. It is merely a way of how string literals are interpreted.
      • Example:
        • As of now, concatenating "\" + "n" will result in string "\n" being a literal backslash \ followed by a literal letter n. (BC break)
        • A string literal "\n" is still expanded to a newline symbol (i.e. line feed or decimal ASCII 10).
    • Currently supported escape sequences are these:
      • \n: Will result in a newline.
      • \t: Will result in TAB character.
      • \\: Will result in a single backslash \.
      • \": Will result in " in both single and double quoted string literals,
      • \': Will result in ' in both single and double quoted string literals,
      • **All other characters with a single backslash in front of them, for example \m, will result in an Unrecognized string escape sequence '\m' error. (BC break)
    • Thanks to @fredsted for the original idea.
  • New: Support for conditional expressions (akin to ternary operator) like "X if (bool) else Y". (Yes, obviously inspired by Python.)
    • Example:
       x = true
       y = 4 if (x) else 2
       // y == 4
       z = false
       y = y if (z) else 6
       // y == 6
  • New: Number literals can now be written with underscores. For example 20_000_000.000_12 can be written instead of 20000000.00012.
  • New: Array values can now be compared against another array values (by comparing all the items in both arrays).
  • New: Regex values can be compared against other Regex values. Comparison returns true if both regexes are exactly the same.
  • New: Brand-new StandardExtension providing general-purpose functions.
    • assert(bool[, string]): Throws error (with optional description as second argument) if the bool is false. This is useful for tests.
    • print(value): Prints the value if Primi is running in CLI.
  • Improved: Optimized iterating over String value.
  • Fixed grammar
    • Variable name 'return_whatever' was parsed as 'return' statement with the additional '_whatever' suffix - causing a syntax error. This is fixed now.
    • It was not possible to place //comments inside array definition that spanned multiple lines. That's possible now.
  • Primi now supports (and also requires) PHP 7.1.

Internals

  • REPL: Do not cache AST when in REPL mode.
  • Separate ::getStringValue() and ::getStringRepr() methods for Value classes.
    • Idea behind this is similar to Python's __str__ and __repr__ dunders.
    • Example for StringValue containing string ahoj:
      • ::getStringValue() returns ahoj.
        • This is what is printed when used with print() function.
      • ::getStringRepr() return "ahoj" (including the double quotes).
        • This is what REPL displays to the user as the expression's result.
    • Function's "string repr" now includes info whether it is a userland function or it represents a native PHP function.
  • Optimization: Text information in AST is now cached only for nodes that explicitly need it (using NODE_NEEDS_TEXT constant in handler class).
  • Optimization: Bunch of stuff that node handlers need to prepare before handling their AST node was moved from runtime to parsing time.
  • Optimization: NumberValue::isNumericInt() helper method now simplified by using ctype_digit() function.
  • Using newer version of smuuf/php-peg that has some new optimizations.
  • Refactoring and reworking tests:
    • The examples in examples directory ARE the language tests.
    • Rewritten many tests and examples to use the new assert() function.
  • New benchmark system:
    • Two files that do roughly the same thing, one written in Primi and one written in PHP.
    • Primi performance is now evaluated "how many times is Primi slower than an equivalent code in PHP?"
    • This should make benchmarks more relevant - and any performance improvements or regressions should be more recognizable.
  • Tests: Using GitHub Actions to run tests now (goodbye, Travis CI!).