diff options
Diffstat (limited to 'ast.h')
-rw-r--r-- | ast.h | 85 |
1 files changed, 85 insertions, 0 deletions
@@ -0,0 +1,85 @@ +#pragma once + +#include <iostream> +#include <string> +#include <vector> +#include <cstdlib> +#include "global.h" +#include "indirect.h" + +using namespace std; + + +class AST; + + +using Number = i64; +using String = string; +using Name = string; +using Index = i64; +class Terms : public vector<AST>{ +public: + using vector<AST>::vector; + using vector<AST>::operator=; +}; +class Lambda{ +public: + Name arg; // if empty, then can only be referred to with indices + Indirect<AST> body=Indirect<AST>::makeEmpty(); + + Lambda(); + Lambda(const Name &arg,const AST &ast); +}; +using Native = function<AST(const AST&)>; + + + +class ParseError : public runtime_error{ +public: + explicit ParseError(const string &what_arg); + explicit ParseError(const char *what_arg); +}; + +class AST{ +public: + enum class Type{ + number, + string, + name, + index, + tuple, + lambda, + native, + }; + + Type type; + Number numval; + String strval; + Name nameval; + Index indexval; + Terms terms; + Lambda lambdaval; + Native nativeval; + + bool quoted=false; + +private: + class Tokeniser; + AST& parse(Tokeniser &tokeniser); + +public: + AST(); // initialises to nil value () + explicit AST(const string &source); // parses source + explicit AST(const char *source); // parses source + + static AST makeNumber(Number numval); + static AST makeString(String strval); + static AST makeName(Name nameval); + static AST makeIndex(Index indexval); + static AST makeTuple(Terms terms); + static AST makeLambda(Lambda lambdaval); + static AST makeLambda(const Name &arg,const AST &body); + static AST makeNative(const Native &native); +}; + +ostream& operator<<(ostream &os,const AST &ast); |