All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
compile.h
Go to the documentation of this file.
1 #ifndef uscript_compile_h
2 #define uscript_compile_h
3 
4 #include <unordered_set>
5 
6 #include "vm.h"
7 #include "chunk.h"
8 #include "scanner.h"
9 #include "tclass.h"
10 
11 namespace uscript {
12 
13 class Compiler {
14  struct Parser {
17  bool hadError;
18  bool panicMode;
19  };
20 
21  enum Precedence {
24  PREC_OR, // or
25  PREC_AND, // and
26  PREC_EQUALITY, // == !=
27  PREC_COMPARISON, // < > <= >=
28  PREC_TERM, // + -
29  PREC_FACTOR, // * /
30  PREC_UNARY, // ! -
31  PREC_CALL, // . ()
33  };
34 
35  using ParseFn = void (Compiler::*)(bool);
36  // typedef void (Compiler::*ParseFn)();
37 
38  struct ParseRule {
42  };
43 
44  struct Local {
46  int depth;
47  };
48 
49  std::unordered_set<std::string> strings;
53  std::vector<Local> locals;
55 
57 
58  const char *source;
59 
60  void Declaration();
61  void Statement();
62  void Synchronize();
63  void Block();
64 
65  void BeginScope();
66  void EndScope();
67 
68  void VarDeclaration();
69  uint8_t IdentifierConstant(Token *token);
70  uint8_t ParseVariable(const char *errorMessage);
71  void DefineVariable(uint8_t global);
72  void DeclareVariable();
73  void AddLocal(Token name);
74  int ResolveLocal(Token *name);
75  void MarkIntialized();
76  void FieldsStatement();
77  void PrintStatement();
78  void ExpressionStatement();
79  void IfStatement();
80  void WhileStatement();
81  void ForStatement();
82  void ReturnStatement();
83 
84  bool Match(TokenType type);
85  bool Check(TokenType type);
86 
87  void ErrorAt(Token &token, const char *message);
88  void PatchJump(int offset);
89  int EmitJump(uint8_t instruction);
90  void EmitLoop(int loopStart);
91  void EmitByte(uint8_t byte);
92  void EmitBytes(uint8_t bytea, uint8_t byteb);
93  void EmitReturn();
94  void EmitConstant(Value value);
95 
96  uint8_t MakeConstant(Value value);
97 
98  void Number(bool canAssign);
99  void Grouping(bool canAssign);
100  void Unary(bool canAssign);
101  void Binary(bool canAssign);
102  void Literal(bool canAssign);
103  void String(bool canAssign);
104  void Variable(bool canAssign);
105  void And(bool canAssign);
106  void Or(bool canAssign);
107  void Call(bool canAssign);
108  void Index(bool canAssign);
109  void Dot(bool canAssign);
110 
111  void NamedVariable(Token token, bool canAssign);
112 
113  void ParsePrecedence(Precedence precedence);
114 
116 
117  uint8_t ArgumentList();
118 
119  void Advance();
120  void Expression();
121  void Consume(TokenType type, const char *message);
122  void SetChunk(Chunk *chunk) { current = chunk; }
123  void Finish();
124  Chunk *CurrentChunk() { return current; }
125 
126  Compiler();
127 
128  bool DoCompile(const char *_source, Chunk *chunk);
129  const char *DoIntern(const std::string &str);
130  void DoRegister(const char *classname);
131 
132  static Compiler &Instance() {
133  static Compiler c;
134  return c;
135  }
136 
137 public:
138  static const char *Intern(const std::string &str) { return Instance().DoIntern(str); }
139  static bool HadError() { return Instance().parser.hadError; }
140  static bool Compile(const char *source, Chunk *chunk) { return Instance().DoCompile(source, chunk); }
141 
142  static TClassInfo *GetClassInfo(const char *classname) {
143  return Instance().tclasslist.Add(classname);
144  }
145 
146  template <typename TObj>
147  static void inline Register() {
148  Instance().DoRegister(std::string(type_name<TObj>()).c_str());
149  }
150 
151 };
152 
153 
154 } // end namespace
155 #endif
static bool HadError()
Definition: compile.h:139
std::unordered_set< std::string > strings
Definition: compile.h:49
bool Check(TokenType type)
Definition: compile.cc:344
const char * DoIntern(const std::string &str)
Definition: compile.cc:673
void Expression()
Definition: compile.cc:394
void ReturnStatement()
Definition: compile.cc:253
BEGIN_PROLOG TPC Trig offset(g4 rise time) ProjectToHeight
Definition: CORSIKAGen.fcl:7
void AddLocal(Token name)
Definition: compile.cc:108
uint8_t IdentifierConstant(Token *token)
Definition: compile.cc:61
void EmitByte(uint8_t byte)
Definition: compile.cc:359
void EmitLoop(int loopStart)
Definition: compile.cc:348
void DefineVariable(uint8_t global)
Definition: compile.cc:122
void Call(bool canAssign)
Definition: compile.cc:504
void Index(bool canAssign)
Definition: compile.cc:498
bool DoCompile(const char *_source, Chunk *chunk)
Definition: compile.cc:8
void DeclareVariable()
Definition: compile.cc:91
const char * source
Definition: compile.h:58
void Consume(TokenType type, const char *message)
Definition: compile.cc:409
void Literal(bool canAssign)
Definition: compile.cc:534
uint8_t ParseVariable(const char *errorMessage)
Definition: compile.cc:67
void Binary(bool canAssign)
Definition: compile.cc:575
static constexpr bool
void BeginScope()
Definition: compile.cc:306
Chunk * current
Definition: compile.h:52
void PrintStatement()
Definition: compile.cc:326
void MarkIntialized()
Definition: compile.cc:118
int EmitJump(uint8_t instruction)
Definition: compile.cc:281
void PatchJump(int offset)
Definition: compile.cc:288
void EmitBytes(uint8_t bytea, uint8_t byteb)
Definition: compile.cc:363
void NamedVariable(Token token, bool canAssign)
Definition: compile.cc:553
void Dot(bool canAssign)
Definition: compile.cc:491
void IfStatement()
Definition: compile.cc:266
static Compiler & Instance()
Definition: compile.h:132
Chunk * CurrentChunk()
Definition: compile.h:124
void Or(bool canAssign)
Definition: compile.cc:509
int ResolveLocal(Token *name)
Definition: compile.cc:79
void FieldsStatement()
Definition: compile.cc:320
std::vector< Local > locals
Definition: compile.h:53
void Declaration()
Definition: compile.cc:35
void ForStatement()
Definition: compile.cc:201
void EmitConstant(Value value)
Definition: compile.cc:434
void SetChunk(Chunk *chunk)
Definition: compile.h:122
Scanner scanner
Definition: compile.h:51
uint8_t MakeConstant(Value value)
Definition: compile.cc:438
uint8_t ArgumentList()
Definition: compile.cc:475
bool Match(TokenType type)
Definition: compile.cc:338
void ErrorAt(Token &token, const char *message)
Definition: compile.cc:386
TClassList tclasslist
Definition: compile.h:56
void DoRegister(const char *classname)
Definition: compile.cc:678
j template void())
Definition: json.hpp:3108
void Grouping(bool canAssign)
Definition: compile.cc:460
TokenType
Definition: scanner.h:8
void WhileStatement()
Definition: compile.cc:183
static const char * Intern(const std::string &str)
Definition: compile.h:138
void Synchronize()
Definition: compile.cc:131
void Number(bool canAssign)
Definition: compile.cc:447
byte_as<> byte
Type of data size stored in bytes, in long long precision.
Definition: datasize.h:98
TClassInfo * Add(const char *classname)
Definition: tclass.cc:16
void And(bool canAssign)
Definition: compile.cc:465
void Variable(bool canAssign)
Definition: compile.cc:549
static TClassInfo * GetClassInfo(const char *classname)
Definition: compile.h:142
void String(bool canAssign)
Definition: compile.cc:544
void Unary(bool canAssign)
Definition: compile.cc:520
void ParsePrecedence(Precedence precedence)
Definition: compile.cc:598
then echo fcl name
temporary value
static bool Compile(const char *source, Chunk *chunk)
Definition: compile.h:140
void Statement()
Definition: compile.cc:154
Parser parser
Definition: compile.h:50
void ExpressionStatement()
Definition: compile.cc:332
ParseRule * GetRule(TokenType type) const
Definition: compile.cc:621
void VarDeclaration()
Definition: compile.cc:46
void(Compiler::*)(bool) ParseFn
Definition: compile.h:35
static void Register()
Definition: compile.h:147
void EmitReturn()
Definition: compile.cc:430