SCPP
A simple scripting language in C++
scpp_ast.hpp
Go to the documentation of this file.
1 
6 #pragma once
7 #include <string>
8 #include <list>
9 #include <map>
10 namespace SCPP
11 {
12  using std::list;
13  using std::map;
14  using std::string;
15 
20  enum class ExprType
21  {
22  Int,
23  Not,
24  Bin,
25  Seq,
26  Assign,
27  Ident,
28  If,
29  While,
30  For,
31  Call,
32  };
33 
38  struct Expr
39  {
40  enum ExprType type;
41  union
42  {
43  struct SInt *i;
44  struct SNot *n;
45  struct SBin *b;
46  struct SSeq *s;
47  struct SAssign *a;
48  struct SIdent *id;
49  struct SIf *if_;
50  struct SWhile *w;
51  struct SFor *f;
52  struct SCall *c;
53  } u;
54  Expr() {}
55  };
56 
57  /* Int */
58  struct SInt
59  {
60  int value;
61  SInt(int value);
62  };
63  SInt::SInt(int value) : value(value)
64  {
65  }
66 
67  /* Unaray Operation */
68  struct SNot
69  {
70  struct Expr expr;
71  SNot(struct Expr expr);
72  };
73  SNot::SNot(struct Expr expr) : expr(expr)
74  {
75  }
76 
77  /* Binominal Operations */
82  enum class Op
83  {
84  Add, // +
85  Sub, // -
86  Mul, // *
87  Div, // /
88  Mod, // %
89  And, // &&
90  Or, // ||
91  Nor, // !|
92  Nand, // !&
93  Xor, // ^^
94  Lt, // <
95  Leq, // <=
96  Gt, // >
97  Geq, // >=
98  Eq, // ==
99  Neq, // !=
100  };
101 
102  struct SBin
103  {
104  struct Expr left;
105  struct Expr right;
106  enum Op op;
107  SBin(struct Expr left, struct Expr right, enum Op op);
108  };
109  SBin::SBin(struct Expr left, struct Expr right, enum Op op) : left(left), right(right), op(op)
110  {
111  }
112 
113  /* Sequence */
114  struct SSeq
115  {
116  list<struct Expr> exprs;
117  SSeq(list<struct Expr> exprs);
118  };
119  SSeq::SSeq(list<struct Expr> exprs) : exprs(exprs)
120  {
121  }
122 
123  /* Variables */
124  struct SAssign
125  {
126  string name;
127  struct Expr value;
128  SAssign(string name, struct Expr value);
129  };
130  SAssign::SAssign(string name, struct Expr value) : name(name), value(value)
131  {
132  }
133 
134  struct SIdent
135  {
136  string name;
137  SIdent(string name);
138  };
139  SIdent::SIdent(string name) : name(name)
140  {
141  }
142 
143  /* If */
144  struct SIf
145  {
146  struct Expr condition;
147  struct Expr thenClause;
148  struct Expr elseClause;
149  SIf(struct Expr condition, struct Expr thenClause, struct Expr elseClause);
150  };
151  SIf::SIf(struct Expr condition, struct Expr thenClause, struct Expr elseClause) : condition(condition), thenClause(thenClause), elseClause(elseClause)
152  {
153  }
154 
155  /* While */
156  struct SWhile
157  {
158  struct Expr condition;
159  struct Expr body;
160  SWhile(struct Expr condition, struct Expr body);
161  };
162  SWhile::SWhile(struct Expr condition, struct Expr body) : condition(condition), body(body)
163  {
164  }
165 
166  /* For */
167  struct SFor
168  {
169  struct Expr init;
170  struct Expr condition;
171  struct Expr update;
172  struct Expr body;
173  SFor(struct Expr init, struct Expr condition, struct Expr update, struct Expr body);
174  };
175  SFor::SFor(struct Expr init, struct Expr condition, struct Expr update, struct Expr body) : init(init), condition(condition), update(update), body(body)
176  {
177  }
178 
179  /* Function */
184  struct SProgram
185  {
186  list<struct SFunction> functions;
187  list<struct Expr> bodies;
188  SProgram(list<struct SFunction> functions, list<struct Expr> bodies);
189  };
190  SProgram::SProgram(list<struct SFunction> functions, list<struct Expr> bodies) : functions(functions), bodies(bodies)
191  {
192  }
193 
198  struct SFunction
199  {
200  string name;
201  list<string> args;
202  struct Expr body;
203  SFunction(string name, list<string> args, struct Expr body);
204  };
205  SFunction::SFunction(string name, list<string> args, struct Expr body) : name(name), args(args), body(body)
206  {
207  }
208 
209  struct SCall
210  {
211  string name;
212  list<struct Expr> args;
213  SCall(string name, list<struct Expr> args);
214  };
215  SCall::SCall(string name, list<struct Expr> args) : name(name), args(args)
216  {
217  }
218 
219  /* Supporting functions */
226  struct Expr tInt(int value)
227  {
228  struct Expr expr;
229  expr.type = ExprType::Int;
230  expr.u.i = new SInt(value);
231  return expr;
232  }
233 
240  struct Expr tNot(struct Expr expr)
241  {
242  struct Expr result;
243  result.type = ExprType::Not;
244  result.u.n = new SNot(expr);
245  return result;
246  }
247 
255  struct Expr tAdd(struct Expr left, struct Expr right)
256  {
257  struct Expr expr;
258  expr.type = ExprType::Bin;
259  expr.u.b = new SBin(left, right, Op::Add);
260  return expr;
261  }
262 
270  struct Expr tSub(struct Expr left, struct Expr right)
271  {
272  struct Expr expr;
273  expr.type = ExprType::Bin;
274  expr.u.b = new SBin(left, right, Op::Sub);
275  return expr;
276  }
277 
285  struct Expr tMul(struct Expr left, struct Expr right)
286  {
287  struct Expr expr;
288  expr.type = ExprType::Bin;
289  expr.u.b = new SBin(left, right, Op::Mul);
290  return expr;
291  }
292 
302  struct Expr tDiv(struct Expr left, struct Expr right)
303  {
304  struct Expr expr;
305  expr.type = ExprType::Bin;
306  expr.u.b = new SBin(left, right, Op::Div);
307  return expr;
308  }
309 
317  struct Expr tMod(struct Expr left, struct Expr right)
318  {
319  struct Expr expr;
320  expr.type = ExprType::Bin;
321  expr.u.b = new SBin(left, right, Op::Mod);
322  return expr;
323  }
324 
334  struct Expr tAnd(struct Expr left, struct Expr right)
335  {
336  struct Expr expr;
337  expr.type = ExprType::Bin;
338  expr.u.b = new SBin(left, right, Op::And);
339  return expr;
340  }
341 
351  struct Expr tOr(struct Expr left, struct Expr right)
352  {
353  struct Expr expr;
354  expr.type = ExprType::Bin;
355  expr.u.b = new SBin(left, right, Op::Or);
356  return expr;
357  }
358 
368  struct Expr tNor(struct Expr left, struct Expr right)
369  {
370  struct Expr expr;
371  expr.type = ExprType::Bin;
372  expr.u.b = new SBin(left, right, Op::Nor);
373  return expr;
374  }
375 
385  struct Expr tNand(struct Expr left, struct Expr right)
386  {
387  struct Expr expr;
388  expr.type = ExprType::Bin;
389  expr.u.b = new SBin(left, right, Op::Nand);
390  return expr;
391  }
392 
402  struct Expr tXor(struct Expr left, struct Expr right)
403  {
404  struct Expr expr;
405  expr.type = ExprType::Bin;
406  expr.u.b = new SBin(left, right, Op::Xor);
407  return expr;
408  }
409 
418  struct Expr tLt(struct Expr left, struct Expr right)
419  {
420  struct Expr expr;
421  expr.type = ExprType::Bin;
422  expr.u.b = new SBin(left, right, Op::Lt);
423  return expr;
424  }
425 
434  struct Expr tLeq(struct Expr left, struct Expr right)
435  {
436  struct Expr expr;
437  expr.type = ExprType::Bin;
438  expr.u.b = new SBin(left, right, Op::Leq);
439  return expr;
440  }
441 
450  struct Expr tGt(struct Expr left, struct Expr right)
451  {
452  struct Expr expr;
453  expr.type = ExprType::Bin;
454  expr.u.b = new SBin(left, right, Op::Gt);
455  return expr;
456  }
457 
466  struct Expr tGeq(struct Expr left, struct Expr right)
467  {
468  struct Expr expr;
469  expr.type = ExprType::Bin;
470  expr.u.b = new SBin(left, right, Op::Geq);
471  return expr;
472  }
473 
482  struct Expr tEq(struct Expr left, struct Expr right)
483  {
484  struct Expr expr;
485  expr.type = ExprType::Bin;
486  expr.u.b = new SBin(left, right, Op::Eq);
487  return expr;
488  }
489 
498  struct Expr tNeq(struct Expr left, struct Expr right)
499  {
500  struct Expr expr;
501  expr.type = ExprType::Bin;
502  expr.u.b = new SBin(left, right, Op::Neq);
503  return expr;
504  }
505 
513  template <class... Args>
514  struct Expr tSeq(Args... args)
515  {
516  list<struct Expr> exprs;
517  for (struct Expr expr : std::initializer_list<struct Expr>{args...})
518  {
519  exprs.push_back(expr);
520  }
521  struct Expr expr;
522  expr.type = ExprType::Seq;
523  expr.u.s = new SSeq(exprs);
524  return expr;
525  }
526 
536  struct Expr tAssign(string name, struct Expr value)
537  {
538  struct Expr expr;
539  expr.type = ExprType::Assign;
540  expr.u.a = new SAssign(name, value);
541  return expr;
542  }
543 
551  struct Expr tIdent(string name)
552  {
553  struct Expr expr;
554  expr.type = ExprType::Ident;
555  expr.u.id = new SIdent(name);
556  return expr;
557  }
558 
568  struct Expr tIf(struct Expr condition, struct Expr thenClause, struct Expr elseClause = tInt(0)) /* conditionが満たされずelseClauseが与えられていない場合、0を返す */
569  {
570  struct Expr expr;
571  expr.type = ExprType::If;
572  expr.u.if_ = new SIf(condition, thenClause, elseClause);
573  return expr;
574  }
575 
584  struct Expr tWhile(struct Expr condition, struct Expr body)
585  {
586  struct Expr expr;
587  expr.type = ExprType::While;
588  expr.u.w = new SWhile(condition, body);
589  return expr;
590  }
591 
602  struct Expr tFor(struct Expr init, struct Expr condition, struct Expr update, struct Expr body)
603  {
604  struct Expr expr;
605  expr.type = ExprType::For;
606  expr.u.f = new SFor(init, condition, update, body);
607  return expr;
608  }
609 
621  template <class... Args>
622  struct SProgram tProgram(list<struct SFunction> functions, Args... bodies)
623  {
624  list<struct Expr> exprs;
625  for (struct Expr expr : std::initializer_list<struct Expr>{bodies...})
626  {
627  exprs.push_back(expr);
628  }
629  return SProgram(functions, exprs);
630  }
631 
640  template <class... Args>
641  list<struct SFunction> FunctionList(Args... functions)
642  {
643  list<struct SFunction> funcList;
644  for (struct SFunction func : std::initializer_list<struct SFunction>{functions...})
645  {
646  funcList.push_back(func);
647  }
648  return funcList;
649  }
659  template <class... Args>
660  struct SFunction tFunction(string name, list<string> args, Args... bodies)
661  {
662  return SFunction(name, args, tSeq(bodies...));
663  }
664 
673  template <class... Args>
674  list<string> ParamList(Args... args)
675  {
676  list<string> argList;
677  for (string arg : std::initializer_list<string>{args...})
678  {
679  argList.push_back(arg);
680  }
681  return argList;
682  }
683 
692  template <class... Args>
693  struct Expr tCall(string name, Args... args)
694  {
695  list<struct Expr> exprs;
696  for (struct Expr expr : std::initializer_list<struct Expr>{args...})
697  {
698  exprs.push_back(expr);
699  }
700  struct Expr expr;
701  expr.type = ExprType::Call;
702  expr.u.c = new SCall(name, exprs);
703  return expr;
704  }
705 
706 } // namespace SCPP
SCPP::tFor
struct Expr tFor(struct Expr init, struct Expr condition, struct Expr update, struct Expr body)
for式を作成する
Definition: scpp_ast.hpp:602
SCPP::tAssign
struct Expr tAssign(string name, struct Expr value)
変数代入式を作成する
Definition: scpp_ast.hpp:536
SCPP::tFunction
struct SFunction tFunction(string name, list< string > args, Args... bodies)
関数定義を作成する
Definition: scpp_ast.hpp:660
SCPP::SProgram
プログラムを表す構造体
Definition: scpp_ast.hpp:184
SCPP::SBin
Definition: scpp_ast.hpp:102
SCPP::ExprType
ExprType
式の種別を表す列挙体
Definition: scpp_ast.hpp:20
SCPP::ParamList
list< string > ParamList(Args... args)
引数のリストを作成する
Definition: scpp_ast.hpp:674
SCPP::tCall
struct Expr tCall(string name, Args... args)
関数呼び出し式を作成する
Definition: scpp_ast.hpp:693
SCPP::tEq
struct Expr tEq(struct Expr left, struct Expr right)
比較演算(等しい)の式を作成する
Definition: scpp_ast.hpp:482
SCPP::SInt
Definition: scpp_ast.hpp:58
SCPP::tNor
struct Expr tNor(struct Expr left, struct Expr right)
論理NORの式を作成する
Definition: scpp_ast.hpp:368
SCPP::SIf
Definition: scpp_ast.hpp:144
SCPP::SFunction
関数を表す構造体
Definition: scpp_ast.hpp:198
SCPP::tXor
struct Expr tXor(struct Expr left, struct Expr right)
論理XORの式を作成する
Definition: scpp_ast.hpp:402
SCPP::FunctionList
list< struct SFunction > FunctionList(Args... functions)
関数リストを作成する
Definition: scpp_ast.hpp:641
SCPP::tProgram
struct SProgram tProgram(list< struct SFunction > functions, Args... bodies)
一つのプログラムを表す構造体を作成する
Definition: scpp_ast.hpp:622
SCPP::tOr
struct Expr tOr(struct Expr left, struct Expr right)
論理ORの式を作成する
Definition: scpp_ast.hpp:351
SCPP::tNeq
struct Expr tNeq(struct Expr left, struct Expr right)
比較演算(等しくない)の式を作成する
Definition: scpp_ast.hpp:498
SCPP::tWhile
struct Expr tWhile(struct Expr condition, struct Expr body)
while式を作成する
Definition: scpp_ast.hpp:584
SCPP::tSeq
struct Expr tSeq(Args... args)
連接式を作成する
Definition: scpp_ast.hpp:514
SCPP::tLt
struct Expr tLt(struct Expr left, struct Expr right)
比較演算(未満)の式を作成する
Definition: scpp_ast.hpp:418
SCPP::SFor
Definition: scpp_ast.hpp:167
SCPP::tNot
struct Expr tNot(struct Expr expr)
論理否定の式を作成する
Definition: scpp_ast.hpp:240
SCPP::tDiv
struct Expr tDiv(struct Expr left, struct Expr right)
除算の式を作成する
Definition: scpp_ast.hpp:302
SCPP::tInt
struct Expr tInt(int value)
整数型の式を作成する
Definition: scpp_ast.hpp:226
SCPP::tGeq
struct Expr tGeq(struct Expr left, struct Expr right)
比較演算(以上)の式を作成する
Definition: scpp_ast.hpp:466
SCPP::tNand
struct Expr tNand(struct Expr left, struct Expr right)
論理NANDの式を作成する
Definition: scpp_ast.hpp:385
SCPP::tLeq
struct Expr tLeq(struct Expr left, struct Expr right)
比較演算(以下)の式を作成する
Definition: scpp_ast.hpp:434
SCPP::tSub
struct Expr tSub(struct Expr left, struct Expr right)
減算の式を作成する
Definition: scpp_ast.hpp:270
SCPP::SNot
Definition: scpp_ast.hpp:68
SCPP::tIf
struct Expr tIf(struct Expr condition, struct Expr thenClause, struct Expr elseClause=tInt(0))
if式を作成する
Definition: scpp_ast.hpp:568
SCPP::tGt
struct Expr tGt(struct Expr left, struct Expr right)
比較演算(より大きい)の式を作成する
Definition: scpp_ast.hpp:450
SCPP::SCall
Definition: scpp_ast.hpp:209
SCPP::Op
Op
二項演算の演算子を表す列挙体
Definition: scpp_ast.hpp:82
SCPP::tAdd
struct Expr tAdd(struct Expr left, struct Expr right)
加算の式を作成する
Definition: scpp_ast.hpp:255
SCPP::SSeq
Definition: scpp_ast.hpp:114
SCPP::SWhile
Definition: scpp_ast.hpp:156
SCPP::tMul
struct Expr tMul(struct Expr left, struct Expr right)
乗算の式を作成する
Definition: scpp_ast.hpp:285
SCPP::tAnd
struct Expr tAnd(struct Expr left, struct Expr right)
論理ANDの式を作成する
Definition: scpp_ast.hpp:334
SCPP::tIdent
struct Expr tIdent(string name)
変数参照式を作成する
Definition: scpp_ast.hpp:551
SCPP::SIdent
Definition: scpp_ast.hpp:134
SCPP::SAssign
Definition: scpp_ast.hpp:124
SCPP::Expr
式を表す構造体
Definition: scpp_ast.hpp:38
SCPP::tMod
struct Expr tMod(struct Expr left, struct Expr right)
剰余の式を作成する
Definition: scpp_ast.hpp:317