|
| 1 | +#ifndef DEFS_H |
| 2 | +#define DEFS_H |
| 3 | +#include <string> |
| 4 | + |
| 5 | +using namespace std; |
| 6 | +// PASS 1 will scan the input test and generate a list of symbols (see PASS1) |
| 7 | +// we will assign a type to each allowed character . |
| 8 | +// e.g. character 'h' (ascii 104) is a LETT, character '0' (ascii 48 )is a DIGIT. character (40) is PAR_L (left parenthesis) |
| 9 | +// the following are the possible character types: |
| 10 | +enum KarType {ETX, |
| 11 | + LF, |
| 12 | + TAB, |
| 13 | + BLANK, |
| 14 | + HASHT, |
| 15 | + PAR_L, |
| 16 | + PAR_R, |
| 17 | + DOT, |
| 18 | + TIMES, |
| 19 | + DIV, |
| 20 | + PLUS, |
| 21 | + MINUS, |
| 22 | + EQ, |
| 23 | + GT, |
| 24 | + LT, |
| 25 | + COLON, |
| 26 | + QUEST, // we ignore this operator, becase ':' takes care of ternary, else QUESTION("[?]"), |
| 27 | + LETT, |
| 28 | + DIGIT, |
| 29 | + CR, |
| 30 | + OTHER, |
| 31 | + EXCLA, |
| 32 | + TEST}; |
| 33 | + |
| 34 | +//PASS 2 will scan the output of PASS1 and generate a list of symbols |
| 35 | +enum TokenType { |
| 36 | + // https://en.cppreference.com/w/cpp/language/operator_precedence#cite_note-2 |
| 37 | + // of https://en.wikipedia.org/wiki/Order_of_operations |
| 38 | + COMMENT, |
| 39 | + VARI, |
| 40 | + LIT, //literal |
| 41 | + CHS, // CHS "change sign " was OP_3 |
| 42 | + TimesDiv, // * or /, according to precedence order - was OP5 |
| 43 | + PlusMin, // + or - - was OP6 |
| 44 | + OP_16, // like elvis |
| 45 | + ELV_Q, |
| 46 | + ELV_C, |
| 47 | + BEXPS, |
| 48 | + BEXPE, |
| 49 | + EOT, |
| 50 | + NONE} ; |
| 51 | + |
| 52 | +typedef struct { |
| 53 | + TokenType type; |
| 54 | + int arity; |
| 55 | + string content; |
| 56 | + int cursor; |
| 57 | +} Token; |
| 58 | + |
| 59 | +KarType kartyp[256]; // type of each character |
| 60 | +string KarPP[256]; // string representation of each character |
| 61 | + |
| 62 | +//KarPP[1] = 2; |
| 63 | + |
| 64 | +void initDinges(){ |
| 65 | + for (int8_t j= 1; j<256; j++) {kartyp[j] = OTHER; KarPP[j] = "OTHER";}; |
| 66 | + for (int8_t j= 48; j<58; j++) {kartyp[j] = DIGIT; KarPP[j] = "D";}; |
| 67 | + for (int8_t j= 65; j<91; j++) {kartyp[j] = LETT; KarPP[j] = "L";}; |
| 68 | + for (int8_t j= 97; j<123; j++) {kartyp[j] = LETT; KarPP[j] = "L";}; |
| 69 | + |
| 70 | + kartyp[95] = LETT; KarPP[95] = "L"; |
| 71 | + kartyp[0] = ETX; KarPP[0] = "ETX"; |
| 72 | + kartyp[10] = LF; KarPP[10] = "LF"; |
| 73 | + kartyp[11] = TAB; KarPP[11] = "TAB"; |
| 74 | + kartyp[13] = CR; KarPP[13] = "CR"; |
| 75 | + kartyp[32] = BLANK; KarPP[32] = "BLANK"; |
| 76 | + kartyp[33] = EXCLA; KarPP[33] = "!END!"; |
| 77 | + kartyp[35] = HASHT; KarPP[35] = "HASHT"; |
| 78 | + kartyp[40] = PAR_L; KarPP[40] = "PAR_L"; |
| 79 | + kartyp[41] = PAR_R; KarPP[41] = "PAR_R"; |
| 80 | + kartyp[42] = TIMES; KarPP[42] = "*"; |
| 81 | + kartyp[43] = PLUS; KarPP[43] = "+"; |
| 82 | + kartyp[45] = MINUS; KarPP[45] = "-"; |
| 83 | + kartyp[46] = DOT; KarPP[46] = "."; |
| 84 | + kartyp[47] = DIV; KarPP[47] = "/"; |
| 85 | + kartyp[58] = COLON; KarPP[58] = ":"; |
| 86 | + kartyp[60] = LT; KarPP[60] = "<"; |
| 87 | + kartyp[61] = EQ; KarPP[61] = "="; |
| 88 | + kartyp[62] = GT; KarPP[62] = ">"; |
| 89 | + kartyp[63] = QUEST; KarPP[63] = "?"; |
| 90 | +} |
| 91 | + |
| 92 | + |
| 93 | +bool isa(Token token, vector<TokenType> allowedTypes){ |
| 94 | +// check if the symbol sym is one of the symbols in the list op |
| 95 | + return(count(allowedTypes.begin(), allowedTypes.end(), token.type) > 0); |
| 96 | +}; |
| 97 | + |
| 98 | + bool isaC(char Kar, vector<KarType> allowedTypes){ |
| 99 | + return(count(allowedTypes.begin(), allowedTypes.end(), int(Kar)) > 0); |
| 100 | + }; |
| 101 | + |
| 102 | +// some operator functions |
| 103 | +float add(float x, float y){return (x+y);} |
| 104 | +float invoke (float x, float y, float (*function) (float, float)){return function(x,y);} |
| 105 | + |
| 106 | + |
| 107 | + |
| 108 | + |
| 109 | +#endif // DEFS_H |
0 commit comments