]> gitweb.ps.run Git - toc/blob - src/check.h
generic grammar
[toc] / src / check.h
1 #pragma once\r
2 \r
3 #include "repr.h"\r
4 \r
5 bool checkStmt(\r
6   const Stmt & s,\r
7   std::vector<Struct> structs,\r
8   std::vector<Function> funcs,\r
9   std::vector<Variable> vars)\r
10 {\r
11   return true;\r
12 }\r
13 \r
14 bool checkFunction(\r
15   const Function & f,\r
16   std::vector<Struct> structs,\r
17   std::vector<Function> funcs,\r
18   std::vector<Variable> vars)\r
19 {\r
20   vars.insert(vars.end(), f.parameters.begin(), f.parameters.end());\r
21   vars.insert(vars.end(), f.body.variables.begin(), f.body.variables.end());\r
22   for (auto s : f.body.statements) {\r
23     if (!checkStmt(s, structs, funcs, vars))\r
24       return false;\r
25   }\r
26   return true;\r
27 }\r
28 \r
29 bool checkProgram(const Program & p)\r
30 {\r
31   for (auto f : p.functions) {\r
32     if (!checkFunction(f, p.structs, p.functions, p.variables))\r
33       return false;\r
34   }\r
35   for (auto s : p.structs) {\r
36     std::vector<Variable> vars = p.variables;\r
37     for (auto v : s.members)\r
38       vars.push_back(v);\r
39     for (auto f : s.methods) {\r
40       if (!checkFunction(f, p.structs, p.functions, vars))\r
41         return false;\r
42     }\r
43   }\r
44   return true;\r
45 }