项目作者: fsacer

项目描述 :
Language based on lox from book "Crafting Interpreters" by @munificent
高级语言: Java
项目地址: git://github.com/fsacer/FailLang.git
创建时间: 2017-03-22T18:08:06Z
项目社区:https://github.com/fsacer/FailLang

开源协议:

下载


Fail language

Language based on lox and tweaked from book “Crafting Interpreters” by @munificent.

Properties

  • regular grammar
  • recursive descent parser

Keywords

Constructs

  • if, else
  • for, while, do-while (break, continue)
  • fun
  • class (this, super)

Data types

  • var
  • Boolean: true, false, and, or
  • none

Misc

  • print

Grammar

Statements:

  1. program declaration* EOF ;
  2. declaration classDecl
  3. | funDecl
  4. | varDecl
  5. | statement ;
  6. classDecl "class" IDENTIFIER ( "<" IDENTIFIER )?
  7. "{" function* "}" ;
  8. funDecl "fun" function ;
  9. varDecl "var" IDENTIFIER ( "=" expression )? ";" ;
  10. statement exprStmt
  11. | forStmt
  12. | ifStmt
  13. | printStmt
  14. | returnStmt
  15. | whileStmt
  16. | block;
  17. exprStmt expression ";" ;
  18. forStmt "for" "(" ( varDecl | exprStmt | ";" )
  19. expression? ";"
  20. expression? ")" statement ;
  21. ifStmt "if" "(" expression ")" statement ( "else" statement )? ;
  22. printStmt "print" expression ";" ;
  23. doWhileStmt "do" statement "while" "(" expression ")" ";" ;
  24. whileStmt "while" "(" expression ")" statement ;
  25. breakStmt "break" ";" ;
  26. continueStmt "continue" ";" ;
  27. block "{" declaration* "}" ;

Expressions:

  1. expression comma ;
  2. comma assignment ( "," assignment )*
  3. assignment ( call "." )? ( ( "=" | "+=" | "-=" | "*=" | "/=" | "**=" ) assignment )?
  4. | ternary ;
  5. ternary logic_or ( "?" expression ":" ternary )?
  6. logic_or logic_and ( "or" logic_and )*
  7. logic_and equality ( "and" equality )*
  8. equality comparison ( ( "!=" | "==" ) comparison )*
  9. comparison term ( ( ">" | ">=" | "<" | "<=" ) term )*
  10. term factor ( ( "-" | "+" ) factor )*
  11. factor unary ( ( "/" | "*" ) unary )*
  12. unary ( "!" | "-" | "++" | "--" ) unary
  13. | exponent ;
  14. exponent (prefix "**" unary)
  15. | prefix ;
  16. prefix ("++" | "--") primary
  17. | postfix ;
  18. postfix primary ( "++" | "--" )*
  19. | call ;
  20. call primary ( "(" arguments? ")" | "." IDENTIFIER )* ;
  21. primary "true" | "false" | "none" | "this"
  22. | NUMBER | STRING | IDENTIFIER | "(" expression ")"
  23. | lambda
  24. | "super" "." IDENTIFIER
  25. // Error productions...
  26. | ( "!=" | "==" ) equality
  27. | ( ">" | ">=" | "<" | "<=" ) comparison
  28. | ( "+" ) term
  29. | ( "/" | "*" ) factor
  30. | ("**") exponent;

Other:

  1. arguments expression ( "," expression )* ;
  2. parameters IDENTIFIER ( "," IDENTIFIER )* ;
  3. function IDENTIFIER functionBody;
  4. method function;
  5. static method "class" method;
  6. constructor "init" functionBody; # returns this
  7. functionBody "(" parameters? ")" block ;
  8. lambda "fun" functionBody ;

Notes

Unary ‘+’ operator is not supported.

Currently continue statement does not work as expected for for loops, incrementors must be manually incremented.

Rules

Operator precedence (highest → lowest)

  1. Name Operators Associates
  2. Call a() Left
  3. Postfix a++ a-- Left
  4. Prefix ++a --a Right
  5. Exponent ** Right
  6. Unary ! - Right
  7. Factor / * Left
  8. Term - + Left
  9. Comparison > >= < <= Left
  10. Equality == != Left
  11. Logical And and Left
  12. Logical Or or Left
  13. Ternary ?: Right
  14. Assignment =, +=, -=, /=, *=, **= Right
  15. Comma , Left

Truthyness

Fail follows Ruby’s simple rule: false and none are falsey and everything else is truthy.

Escape sequences

  1. \" double quote
  2. \\ single backslash
  3. \b backspace
  4. \r carriage return
  5. \n newline
  6. \t tab

Data types

Variables can change it’s data type at runtime as in Python. Data types are implied from expressions.

Built-in data types

  • Boolean: true or false
  • string
  • number (double precision)
  • none - a null pointer

    User defined data types

    Those can be defined via class keyword.

Standard library

Global functions

  • clock() - Gets the difference, measured in milliseconds, between the current time and midnight, January 1, 1970 UTC. Then it changes milliseconds to seconds as a floating point value.
  • len(x) - This function changes input to a string and gets its length.
  • str(x) - Changes input to its string representation.

Added features

Additional features mostly based on tasks from book:

  • multiline comments
  • postfix and prefix increment/decrement operators
  • ternary operator
  • exponent operator
  • prevent access to unassigned variables (no implicit initialization to none)
  • accept escape sequences
  • comma operator
  • operator overload for string multiplication (“abc” * 2 → “abcabc”)
  • break and continue
  • do-while statement
  • shorthand assignment operators +=, -=, =, /=, *=
  • prevention of assignment inside if, loop and ternary condition expressions
  • lambdas
  • warnings, if local variable is unused
  • static methods, getters and setters