From b3ad2e88c9462391fb6d323f6f7737ed5ceedb46 Mon Sep 17 00:00:00 2001 From: tomsmeding Date: Fri, 3 Feb 2017 23:09:04 +0100 Subject: Progress --- Makefile | 2 +- parser.cpp | 105 ++++++++++++++++++++++++++++++++++++++++++++++++++++++------- 2 files changed, 95 insertions(+), 12 deletions(-) diff --git a/Makefile b/Makefile index 4e9ed24..5bfd9c0 100644 --- a/Makefile +++ b/Makefile @@ -14,7 +14,7 @@ remake: clean $(TARGET): $(patsubst %.cpp,%.o,$(wildcard *.cpp)) - $(CXX) -o $@ $< + $(CXX) -o $@ $^ %.o: %.cpp $(wildcard *.h) $(CXX) $(CXXFLAGS) -c -o $@ $< diff --git a/parser.cpp b/parser.cpp index 73ae368..56fb035 100644 --- a/parser.cpp +++ b/parser.cpp @@ -1,4 +1,5 @@ #include +#include #include #include #include @@ -52,6 +53,7 @@ public: number, string, symbol, + terminator, }; Type type; @@ -70,6 +72,14 @@ class Tokeniser{ i64 lnum,linex; Token::Type ttype; + /*struct State{ + i64 idx,nextidx; + i64 lnum,lineidx; + Token::Type ttype; + }; + + stack statestack;*/ + bool eof(i64 at){ return at>=(i64)source.size(); } @@ -88,11 +98,39 @@ public: idx(0),nextidx(-1), lnum(1),linex(1){} + Tokeniser& operator=(const Tokeniser &other){ + if(&source!=&other.source||&filename!=&other.filename){ + throw runtime_error("Tokeniser::operator= on incompatible Tokeniser"); + } + idx=other.idx; nextidx=other.nextidx; + lnum=other.lnum; linex=other.linex; + ttype=other.ttype; + return *this; + } + + /*void save(){ + statestack.push({idx,nextidx,lnum,lineidx,ttype}); + } + + void restore(){ + if(statestack.size()==0)throw runtime_error("Tokeniser::restore() on empty stack"); + const State &st=statestack.top(); + idx=st.idx; nextidx=st.nextidx; + lnum=st.lnum; linex=st.linex; + ttype=st.ttype; + statestack.pop(); + } + + void discardstate(){ + if(statestack.size()==0)throw runtime_error("Tokeniser::discardstate() on empty stack"); + statestack.pop(); + }*/ + bool eof() const { return idx>=(i64)source.size(); } - Token get() const { + Token get(){ return Token(ttype,get_(),Site(filename,lnum,linex)); } @@ -100,6 +138,7 @@ public: bool advance(){ if(eof())return false; + // Let nextidx catch up with idx while(idx