Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <vector>
- #include <fstream>
- #include "lex.h"
- using namespace std;
- Lex getNextToken(istream& in, int& linenum);
- Token getKeywords(string str,Token orig);
- Lex getNextToken(istream& in, int& linenum){
- char newchar = '\0';
- string oper = ";+-!\\*()";
- Token checktoken = ERR;
- string checklex = "";
- int checkline = -1;
- bool flush = false;
- Lex nextLex;
- while(true){
- in.get(newchar);
- if(checktoken == STR)
- {
- if(newchar == '\\')
- {
- char escaped;
- in.get(escaped);
- if(escaped == 'n') {
- checklex += '\n';
- }
- else
- checklex += escaped;
- }
- else if(newchar == '\"')
- {
- checkline = linenum;
- flush = true;
- }
- else if(newchar == EOF || newchar == '\n')
- {
- checktoken = ERR;
- checkline = linenum;
- flush = true;
- }
- else
- checklex += newchar;
- }
- else if(checktoken == INT)
- {
- if(newchar == ' ' || newchar == EOF || newchar == '\n' || oper.find(newchar) != std::string::npos)
- {
- in.unget();
- checkline = linenum;
- flush = true;
- }
- else if(isdigit(newchar))
- checklex += newchar;
- else
- {
- checktoken = ERR;
- checkline = linenum;
- flush = true;
- }
- }
- else if(checktoken == ID)
- {
- if(newchar == ' ' || newchar == EOF || newchar == '\n' || oper.find(newchar) != std::string::npos)
- {
- in.unget();
- checkline = linenum;
- flush = true;
- }
- else if(isalnum(newchar))
- checklex += newchar;
- else
- {
- checktoken = ERR;
- checkline = linenum;
- flush = true;
- }
- }
- else if(checktoken == ERR){
- if(isdigit(newchar))
- {
- checklex += newchar;
- checktoken = INT;
- continue;
- }
- if(isalpha(newchar))
- {
- checklex += newchar;
- checktoken = ID;
- continue;
- }
- switch (newchar) {
- case '\"':
- checktoken = STR;
- break;
- case '/':
- char nextchar;
- nextchar = in.peek();
- if (nextchar == '/') {
- string s;
- while(true)
- {
- char nextchar;
- in.get(nextchar);
- if(nextchar == '\n')
- {
- in.unget();
- break;
- }
- };
- linenum++;
- }
- else {
- checktoken = SLASH;
- checkline = linenum;
- flush = true;
- }
- break;
- case ' ':
- break;
- case '\n':
- linenum++;
- break;
- case EOF:
- checktoken = DONE;
- checkline = linenum;
- flush = true;
- break;
- case '+':
- checktoken = PLUS;
- checkline = linenum;
- flush = true;
- break;
- case '-':
- checktoken = MINUS;
- checkline = linenum;
- flush = true;
- break;
- case '*':
- checktoken = STAR;
- checkline = linenum;
- flush = true;
- break;
- case '!':
- checktoken = BANG;
- checkline = linenum;
- flush = true;
- break;
- case '(':
- checktoken = LPAREN;
- checkline = linenum;
- flush = true;
- break;
- case ')':
- checktoken = RPAREN;
- checkline = linenum;
- flush = true;
- break;
- case ';':
- checktoken = SC;
- checkline = linenum;
- flush = true;
- break;
- }
- }
- if(flush)
- {
- checktoken = getKeywords(checklex,checktoken);
- nextLex = Lex(checktoken,checklex,checkline);
- return nextLex;
- }
- }
- }
- Token getKeywords(string str,Token orig){
- if(orig == ID)
- {
- if(str.compare("print") == 0)
- {
- return PRINT;
- }
- else if(str.compare("if") == 0)
- {
- return IF;
- }
- else if(str.compare("loop") == 0)
- {
- return LOOP;
- }
- else if(str.compare("let") == 0)
- {
- return LET;
- }
- else if(str.compare("begin") == 0)
- {
- return BEGIN;
- }
- else if(str.compare("end") == 0)
- {
- return END;
- }
- }
- return orig;
- }
- int main(int argc, char *argv[]) {
- bool rigged_filename_pos = false;
- string filename = "";
- filename.clear();
- for (int i = 1; i < argc; i++) {
- if (rigged_filename_pos && argv[i][0] == '-')
- {
- cout << "UNRECOGNIZED FLAG " << filename << endl;
- return 1;
- }
- if (i == argc - 1) {
- if (argv[i][0] != '-') {
- if (!rigged_filename_pos) {
- rigged_filename_pos = true;
- filename = string(argv[i]);
- continue;
- } else {
- cout << "ONLY ONE FILE NAME ALLOWED" << endl;
- return 0;
- }
- }
- } else {
- if (argv[i][0] != '-') {
- if (!rigged_filename_pos) {
- rigged_filename_pos = true;
- filename = string(argv[i]);
- continue;
- } else {
- cout << "ONLY ONE FILE NAME ALLOWED" << endl;
- return 0;
- }
- }
- }
- if (string(argv[i]).compare("-v") == 0)
- ;
- else if (string(argv[i]).compare("-consts") == 0)
- ;
- else if (string(argv[i]).compare("-ids") == 0)
- ;
- else {
- cout << "UNRECOGNIZED FLAG " << (argv[i]) << endl;
- return 1;
- }
- }
- fstream fs;
- if(rigged_filename_pos)
- {
- fs.open(filename);
- if(fs.fail())
- {
- cout << "CANNOT OPEN " << filename << endl;
- return 1;
- }
- }
- if(!filename.empty())
- fs.close();
- vector<Lex> ids;
- vector<Lex> consts;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement