summaryrefslogtreecommitdiff
path: root/ast.h
blob: 78abd550ffa043a101fc022917699edac8af234f (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
#pragma once

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

using namespace std;


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

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

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

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

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

	Site site;

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

class Expression{
public:
	enum class Type{
		binop,  // name, args[0], args[1]
		unop,  // name, args[0]
		call,  // name, args
		dive,  // name, args, body
		number,  // numval
		string,  // strval
		scope,  // scope
	};

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

	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 StatementList &body);  // dive
	Expression(Type type,double numval);  // number
	Expression(Type type,const string &strval);  // string
	Expression(Type type,const Scope &scope);  // scope
};

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
};