From 3b390967e7c2ee4ac6d1a67c77f40ed43005e012 Mon Sep 17 00:00:00 2001 From: tomsmeding Date: Sun, 20 Nov 2016 11:27:07 +0100 Subject: Initial --- ast.h | 85 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 ast.h (limited to 'ast.h') diff --git a/ast.h b/ast.h new file mode 100644 index 0000000..c0b0d90 --- /dev/null +++ b/ast.h @@ -0,0 +1,85 @@ +#pragma once + +#include +#include +#include +#include +#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{ +public: + using vector::vector; + using vector::operator=; +}; +class Lambda{ +public: + Name arg; // if empty, then can only be referred to with indices + Indirect body=Indirect::makeEmpty(); + + Lambda(); + Lambda(const Name &arg,const AST &ast); +}; +using Native = function; + + + +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); -- cgit v1.2.3-70-g09d2