computerscare-vcv-modules

computerscare modules for VCV Rack
Log | Files | Refs

commit d7079f6c858657ecb49fe12f345950acdba5bb1b
parent 3c28456e0e91ce06c36c6a3e2bde2f99180ee3ec
Author: Adam Malone <[email protected]>
Date:   Mon, 26 Nov 2018 16:09:23 -0600

begin tokenizer

Diffstat:
Msrc/dtpulse.cpp | 87++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-------
Msrc/dtpulse.hpp | 23++++++++++++++++++++++-
Msrc/test.cpp | 7+------
3 files changed, 103 insertions(+), 14 deletions(-)

diff --git a/src/dtpulse.cpp b/src/dtpulse.cpp @@ -309,17 +309,90 @@ bool matchParens(std::string value) { theyMatch = (parensCount==0) && (squareCount ==0) && (curlyCount==0) && (angleCount==0); return theyMatch; } +void whoKnows(std::string input) { + //std::vector<Token> tStack = tokenizeString(input); + //return evalToken("","Integer",tStack); + AbsoluteSequence abs = AbsoluteSequence(input,knobandinputlookup); + abs.print(); +} - +AbsoluteSequence::AbsoluteSequence(std::string expr, std::string lookup) { + Parser p = Parser(expr); + indexSequence = parseEntireString(expr,lookup,1); +} +void AbsoluteSequence::print() { + printVector(indexSequence); +} Token::Token(std::string t, std::string v) { type = t; val = v; } +Parser::Parser(std::string expr) { + setExpression(expr); + //tokens = tokenizeString(expr); +} +void Parser::setExpression(std::string expr) { + expression=expr; + currentIndex=0; + char c; + while ((c = peekChar())) { + switch (c) { + default: + if((c >= '0' && c <= '9') || c == '.') { + printf("num:%s\n",parseNumber(c).c_str()); + } + else { + + } + } + skipChar(); + } + +} +char Parser::peekChar() { + if (currentIndex < (int) expression.size()) return expression[currentIndex]; + return 0; +} +void Parser::skipChar() { + currentIndex++; + } +char Parser::skipAndPeekChar() { + skipChar(); + return peekChar(); +} +std::string Parser::parseNumber(char c) +{ + std::string number; + if (c != '.') + { + // parse before '.' + while (c != 0 && c >= '0' && c <= '9' && c != '.' ) { + number += c; + c = skipAndPeekChar(); + } + } + if (c == '.') + { + // parse after '.' + number += c; + c = skipAndPeekChar(); + if (c != 0 && c >= '0' && c <= '9') { + while (c != 0 && c >= '0' && c <= '9' ) { + number += c; + c = skipAndPeekChar(); + } + } else { + printf("Expected digit after '.', number: %s\n",number.c_str()); + } + } + return number; +} void Token::print() { printf("type:%s, val:%s\n",type.c_str(),val.c_str()); } std::vector<Token> tokenizeString(std::string input) { std::vector<Token> stack; + int stringIndex=0; for(unsigned int i = 0; i < input.length(); i++) { std::string token(1,input[i]); if(token=="(") stack.push_back(Token("LeftParen",token)); @@ -345,16 +418,16 @@ std::vector<Token> tokenizeString(std::string input) { else if(token== ":") stack.push_back(Token("Colon",token)); else if(token== ";") stack.push_back(Token("Semicolon",token)); else if(token== "|") stack.push_back(Token("Pipe",token)); - else if(knobandinputlookup.find(token) != -1) stack.push_back(Token("Letter",token)); - else if(integerlookup.find(token) != -1) stack.push_back(Token("Integer",token)); + else if(knobandinputlookup.find(token) != -1) { + stack.push_back(Token("Letter",token)); + } + else if(integerlookup.find(token) != -1) { + stack.push_back(Token("Integer",token)); + } else stack.push_back(Token("Unknown",token)); } return stack; } -std::string whoKnows(std::string input) { - std::vector<Token> tStack = tokenizeString(input); - return evalToken("","Integer",tStack); -} std::string evalToken(std::string input,std::string type, std::vector<Token> tStack) { std::string output = input; diff --git a/src/dtpulse.hpp b/src/dtpulse.hpp @@ -14,6 +14,14 @@ extern std::string knoblookup; extern std::string inputlookup; extern std::string knobandinputlookup; #endif +class AbsoluteSequence { + public: + AbsoluteSequence(std::string expr, std::string lookup); + std::vector<int> indexSequence; + std::vector<float> exactFloats; + std::vector<std::vector<int>> randoms; + void print(); +}; class Token { public: std::string type; @@ -21,6 +29,19 @@ class Token { Token(std::string t, std::string v); void print(); }; +class Parser { + public: + Parser(std::string expr); + std::string expression; + std::vector<Token> tokens; + char peekChar(); + char skipAndPeekChar(); + void skipChar(); + void setExpression(std::string expr); + std::string parseNumber(char c); + private: + int currentIndex; +}; bool is_digits(const std::string &str); void padTo(std::string &str, const size_t num, const char paddingChar ); std::vector <int> parseString(std::string expr); @@ -39,4 +60,4 @@ std::string concatVectorFromLookup(std::vector<int> vector, std::string lookup); std::vector<Token> tokenizeString(std::string input); bool matchParens(std::string value); std::string evalToken(std::string input, std::string type,std::vector<Token> tStack); -std::string whoKnows(std::string input); +void whoKnows(std::string input); diff --git a/src/test.cpp b/src/test.cpp @@ -34,12 +34,7 @@ int main(int argc, char** argv) printf("%s\n",strResult.c_str()); } else if(type==5) { - std::vector<Token> stack = tokenizeString(argv[1]); - - for(int i = 0; i < stack.size(); i++) { - stack[i].print(); - } - printf("whoKnows:%s\n",whoKnows(argv[1]).c_str()); + whoKnows(argv[1]); } return 0; }