Commit graph

19 commits

Author SHA1 Message Date
Eric Swanson
5033aefcdc
Add do-while statement (#276)
Adds:
- ast/DoWhileLoop
2020-09-06 02:26:41 -07:00
Eric Swanson
966b3acf5a
Add foreach loops (#275)
Adds:
- ast/ForeachLoop: AST node
2020-09-06 01:22:47 -07:00
Eric Swanson
4a033340bf
Disambiguator: figure out if label is a case statement dispatch selector, or should apply to a statement within the case dispatch group's block. (#273) 2020-09-05 21:52:56 -07:00
Eric Swanson
f3202547fb
New compiler: Add support for case statements (#272)
Adds:
- ast/CaseDispatchDefaultSelector: AST node for a "default:" selector
- ast/CaseDispatchGroup: AST node for a collection of selectors and the code to execute
- ast/CaseDispatchGroups: AST node holding all groups in a case statement
- ast/CaseDispatchSelectors: AST node holding all selectors for one group
- ast/CaseStatement: AST node for a whole case statement
- codegen/CaseDispatchGroupVisitor: knows what to put in a case jump dispatch table
- codegen/CaseJumpDataBlock: knows how to build the entries in the data block

Also, detect multiple case selectors with the same value (or default).  This is different from the OG compiler, which only detected duplicate `default:` selectors.
2020-09-05 20:41:58 -07:00
Eric Swanson
775ef51d91
Fix divide-by-zero errors, and mask FormatError if it were thrown in a destructor. (#271) 2020-09-05 01:19:12 -07:00
Eric Swanson
92ba5be8cf
Add break and continue statements. (#270)
Adds:
- ast/JumpStatement: AST node for break and continue statements
2020-09-04 18:21:08 -07:00
Eric Swanson
1a3619b231
Add support for while loops (#268)
Adds:
- analyzer/FlowControlScope: Registers a break/continue scope
- analyzer/FlowControlScopes: Registry for break/continue scopes
- ast/LabelableStatement: Base class for AST nodes that can be labelled (loops and case)
- ast/LoopStatement: Base class for AST nodes for loops (have a break and continue label)
- ast/WhileLoop: AST node for a while loop
2020-09-03 19:46:27 -07:00
Eric Swanson
6ce9c38de3
Add support for const declarations (#264)
Adds:
- analyzer/Constants: holds constant name -> value for lookup
- ast/ConstDeclaration: AST node for a const declaration
- optimizer/ConstValidator: validates that an optimized expression is valid to use as a constant.

the Optimizer converts Identifiers that refer to a constant to the optimized constant value.
2020-09-01 00:54:24 -07:00
Eric Swanson
6d163918c5
Handle default parameter values, pass-by-name, and return statements in functions. (#263)
Process function call parameters to account for:
- default parameter values
- passing parameters by name

Also handle return within a function (I left this out of the previous PR)

Adds:
- astbuilder/SimpleValueCloner: clones values that are valid as constants and parameter default values.
2020-08-31 23:16:30 -07:00
Eric Swanson
b3bd302785
Add limited support for user functions (#262)
Adds:
- ast/UserFunction AST node for user-defined functions.
- astbuilder/AvailableUserFunction: reference to a parse tree for a user function.
  - The AST builder only generates ASTs for user functions that are actually referenced.
- astbuilder/UserFunctionVisitor: visits (builds an AST for) a parse tree for a user function.
  - This happens after the .src or .inc file has been otherwise processed, so this class serves to hook up the correct SourceFileIdentifier to the AST.
2020-08-31 18:46:27 -07:00
Eric Swanson
95e604e417
Add support for if-then-else statements (#259)
Adds:
- ast/Block: a block scope that allows declaring local variables
- ast/IfThenElseStatement: AST node for if..elseif..else..endif statements
- model/FlowControlLabel: provides an anchor for jumps or calls.
  - Function calls, break statements, continue statements, and loops will all use these.
2020-08-30 17:03:26 -07:00
Eric Swanson
64503c2866
var statements in program blocks (#256)
Adds:
- analyzer/LocalVariableScope: tracks local variables created within a given scope.
  - Instantiated on the stack during semantic analysis, this registers itself with LocalVariableScopes during construction and deregisters itself during destruction.
  - Detects unused variables during deregistration
- analyzer/LocalVariableScopes: this is what LocalVariableScope registers with.
  - keeps track of the stack of local scopes.
  - can provide the current local scope, for var statements.

Other notes:

"Shadowed" variables: this is when a variable in one scope hides a variable in another, like so:
    var a := 2;
    if (a)
        var a := 3;
        a := 4;
    endif
    print(a); // still 2
"debug_variables": We'll use these later when writing debug files.
2020-08-30 01:32:38 -07:00
Eric Swanson
5e57733a68
New compiler: program declarations (#246)
Add:
- FunctionBody
- Program
- ProgramParameterDeclaration
- ProgramParameterList

Build AST for program sections and generate code for it
2020-08-28 22:45:07 -07:00
Eric Swanson
9bbc0518f8
Compiler rewrite: add global variables (#241)
Adds support for var statements at the global level.

Adds:
- analyzer/Variables: keeps track of the variables in scope (either local or global).
- ast/Identifier: AST node for an identifier.
  - The optimizer will replace constant identifiers with their constant value (in a later commit).
  - The semantic analyzer will set the variable field for local or global variable identifiers.
- ast/VarStatement: AST node for a var statement.
  - A single var statement will generate one VarStatement per variable declared.
- model/Variable: Describes a variable, including its index within its scope.
2020-08-25 00:57:03 -07:00
Eric Swanson
3a8ab09a56
Add Expression, Statement, Value AST base classes, and TopLevelStatements (#229)
* Add ast/ Expression, Statement, Value

* Add ast/TopLevelStatements
2020-08-17 22:53:03 -07:00
Eric Swanson
841bc928d3
Normalize include paths (#221) 2020-08-15 08:01:19 +02:00
Eric Swanson
72bd179051
Add analyzer/Disambiguator stub (#205)
The Disambiguator will correct the AST based on context.

At present, it needs to handle ambiguities in the grammar between
case group selectors and named break/continue labels.
2020-08-11 08:21:34 +02:00
Eric Swanson
435db9dc07
bscript/compiler: use c++17 nested namespace definitions (#202) 2020-08-10 12:20:29 +02:00
Eric Swanson
eb6594b4c6
Add ast/NodeVisitor, analyzer/SemanticAnalyzer stubs (#201) 2020-08-10 10:37:07 +02:00