TorqueScript  0.2
compiler.hpp
1 
15 #pragma once
16 
17 #include <memory>
18 
19 #include "antlr4-runtime.h"
20 
21 #include <torquescript/astvisitor.hpp>
22 #include <torquescript/codeblock.hpp>
23 #include <torquescript/stringtable.hpp>
24 #include <torquescript/instructions.hpp>
25 #include <torquescript/interpreterconfiguration.hpp>
26 #include <torquescript/instructionsequence.hpp>
27 
28 namespace TorqueScript
29 {
34  class Compiler : public AST::ASTVisitor
35  {
36  public:
37  Compiler(const InterpreterConfiguration& config);
38 
44  CodeBlock* compileStream(std::istream& input, StringTable* stringTable);
45 
50  CodeBlock* compileString(const std::string& input, StringTable* stringTable);
51 
56  CodeBlock* compileFile(const std::string& path, StringTable* stringTable);
57 
58  virtual antlrcpp::Any defaultResult() override;
59  virtual antlrcpp::Any aggregateResult(antlrcpp::Any& aggregate, antlrcpp::Any& nextResult) override;
60 
61  const InterpreterConfiguration mConfig;
62 
63  private:
64  std::string mCurrentPackage;
65  StringTable* mStringTable;
66 
67  /*
68  Compiler Routines ==============================
69  */
70 
71  virtual antlrcpp::Any visitPackageDeclarationNode(AST::PackageDeclarationNode* package) override;
72  virtual antlrcpp::Any visitFunctionDeclarationNode(AST::FunctionDeclarationNode* function) override;
73  virtual antlrcpp::Any visitFunctionCallNode(AST::FunctionCallNode* call) override;
74  virtual antlrcpp::Any visitSubFunctionCallNode(AST::SubFunctionCallNode* call) override;
75  virtual antlrcpp::Any visitSubFieldNode(AST::SubFieldNode* subfield) override;
76  virtual antlrcpp::Any visitAddNode(AST::AddNode* expression) override;
77  virtual antlrcpp::Any visitIntegerNode(AST::IntegerNode* value) override;
78  virtual antlrcpp::Any visitFloatNode(AST::FloatNode* value) override;
79  virtual antlrcpp::Any visitStringNode(AST::StringNode* value) override;
80  virtual antlrcpp::Any visitTaggedStringNode(AST::TaggedStringNode* value) override;
81  virtual antlrcpp::Any visitLocalVariableNode(AST::LocalVariableNode* value) override;
82  virtual antlrcpp::Any visitGlobalVariableNode(AST::GlobalVariableNode* value) override;
83  virtual antlrcpp::Any visitAssignmentNode(AST::AssignmentNode* expression) override;
84  virtual antlrcpp::Any visitLessThanNode(AST::LessThanNode* expression) override;
85  virtual antlrcpp::Any visitNegateNode(AST::NegateNode* expression) override;
86  virtual antlrcpp::Any visitNotNode(AST::NotNode* expression) override;
87  virtual antlrcpp::Any visitIncrementNode(AST::IncrementNode* expression) override;
88  virtual antlrcpp::Any visitWhileNode(AST::WhileNode* node) override;
89  virtual antlrcpp::Any visitForNode(AST::ForNode* node) override;
90  virtual antlrcpp::Any visitBreakNode(AST::BreakNode* node) override;
91  virtual antlrcpp::Any visitReturnNode(AST::ReturnNode* node) override;
92  virtual antlrcpp::Any visitTernaryNode(AST::TernaryNode* node) override;
93  virtual antlrcpp::Any visitSwitchNode(AST::SwitchNode* node) override;
94  virtual antlrcpp::Any visitIfNode(AST::IfNode* node) override;
95  virtual antlrcpp::Any visitArrayNode(AST::ArrayNode* array) override;
96  virtual antlrcpp::Any visitEqualsNode(AST::EqualsNode* expression) override;
97  virtual antlrcpp::Any visitConcatNode(AST::ConcatNode* expression) override;
98  virtual antlrcpp::Any visitMultiplyNode(AST::MultiplyNode* expression) override;
99  virtual antlrcpp::Any visitDivideNode(AST::DivideNode* expression) override;
100  virtual antlrcpp::Any visitDatablockDeclarationNode(AST::DatablockDeclarationNode* datablock) override;
101  virtual antlrcpp::Any visitObjectDeclarationNode(AST::ObjectDeclarationNode* object) override;
102  virtual antlrcpp::Any visitFieldAssignNode(AST::FieldAssignNode* node) override;
103  };
104 }
Definition: astvisitor.hpp:65
Definition: ast.hpp:278
Definition: ast.hpp:599
Definition: ast.hpp:364
Definition: ast.hpp:710
Definition: ast.hpp:334
Definition: ast.hpp:320
Definition: ast.hpp:350
Definition: ast.hpp:124
Definition: ast.hpp:485
Definition: ast.hpp:654
Definition: ast.hpp:828
Definition: ast.hpp:436
Definition: ast.hpp:469
Definition: ast.hpp:378
Definition: ast.hpp:306
Definition: ast.hpp:408
Definition: ast.hpp:422
Definition: ast.hpp:686
Definition: ast.hpp:501
Definition: ast.hpp:236
Definition: ast.hpp:767
Definition: ast.hpp:724
Definition: ast.hpp:627
A CodeBlock defines a piece of executable code generated from a single input (Ie. a file)....
Definition: codeblock.hpp:37
Primary compiler class. This class is an AST visitor that walks the AST tree generated by a AST::ASTB...
Definition: compiler.hpp:35
CodeBlock * compileStream(std::istream &input, StringTable *stringTable)
Ask the compiler to generate a codeblock from the provided stream.
CodeBlock * compileString(const std::string &input, StringTable *stringTable)
Ask the compiler to generate a codeblock from the provided string.
CodeBlock * compileFile(const std::string &path, StringTable *stringTable)
Ask the compiler to generate a codeblock from the provided file.
A string table maintains a mapping of numeric identifiers to their raw string values....
Definition: stringtable.hpp:32
Definition: ast.hpp:28
A structure representing overall interpreter runtime configuration. Some settings can be changed at r...
Definition: interpreterconfiguration.hpp:26