summaryrefslogtreecommitdiff
path: root/ast.h
blob: de30d5a6331623302aa8ffd660156b9cdd05cff7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#pragma once

#include <iostream>
#include <string>
#include <vector>
#include "global.h"

using namespace std;


class Statement;
class Expression;
using Name = string;
using StatementList = vector<Statement>;

class Site{
public:
	string filename;
	i64 lnum,linex;

	Site();
	Site(const string &filename,i64 lnum,i64 linex);

	Site addX(i64 dx);
};

ostream& operator<<(ostream &os,Site site);

class Scope{
public:
	enum class Type{
		direct,
		lazy,
		function,
		// async,
	};

	Type type;
	StatementList body;
	vector<Expression> args;

	Site site;

	Scope();
	Scope(Type type,const StatementList &body,const vector<Expression> &args);
	// Scope(Type type,const StatementList &body,const vector<Name> &args);
};

ostream& operator<<(ostream &os,const Scope &scope);

class Expression{
public:
	enum class Type{
		binop,  // name, args[0], args[1]
		unop,  // name, args[0]
		call,  // name, args         -- variable reference when args={}
		dive,  // name, args, scope  -- variable dive when args={}
		number,  // numval
		string,  // strval
		scope,  // scope
		cond,  // args[0] (cond), args[1] (then), args[2] (else)
	};

	Type type;
	Name name;
	vector<Expression> args;
	double numval;
	string strval;
	Scope scope;

	Site site;

	// Expression();
	Expression(Type type,const Name &name,const vector<Expression> &args);  // binop, call
	Expression(Type type,const Name &name,const Expression &arg);  // unop
	Expression(Type type,const Name &name,const vector<Expression> &args,const Scope &scope);  // dive
	Expression(Type type,double numval);  // number
	Expression(Type type,const string &strval);  // string
	Expression(Type type,const Scope &scope);  // scope
	Expression(Type type,const vector<Expression> &args);  // cond
};

ostream& operator<<(ostream &os,const Expression &expr);

class Statement{
public:
	enum class Type{
		create,  // dstvar, expr
		assign,  // dstvar, expr
		expression,  // expr
	};

	Type type;
	Name dstvar;
	Expression expr;

	Site site;

	Statement();
	Statement(Type type,const Name &dstvar,const Expression &expr);  // create, assign
	Statement(Type type,const Expression &expr);  // expr
};

ostream& operator<<(ostream &os,const Statement &st);
ostream& operator<<(ostream &os,const StatementList &stl);