Simplified Basic Interpretation of Basic Interpretation (2) --- C ++ Packaging

zhaozj2021-02-08  215

This is the source code of cmake. Mainly responsible for the extraction of the vocabulary You can call it cmake :: get_token (), return CTOKEN's class.

/// Make.h /////

Enum token_types {Delimiter, Variable, Number, Command, String, Quote, Finished, None, Enter}; // Tag Type Collection

#define token_max 80 # define strdelimiter " - * ^ / =; (),> <" Symbol collection #define DIM 11 // DIM # define as 12 // as # define integer 13 // integer # define print 14 //Print

Class CToken {public: char token [token_max]; int token_type; int tok;};

Class cmake {public: cmake (char * prog, int proglength); virtual ~ cmake (); public: char * prog; int proglength; int isdelim (CHAR C); // If the operation symbol returns 1, not returning 0 INT IsWhite (CHAR C); // is a space returns 1, not returning 0 int Look_up (char * c); // Returns the Command type, c is the pointer ctoken get_token (void) of the Command string; // Get tag INT Findchar (char * str, char ch); // Locate CH from Str, returns to its reference in Str; if there is no CH in Str, return -1};

/

// Make.cpp: Implementation of the cmake class.////

#include "stdafx.h" #include "make.h"

//// construction / destruction //

CMake :: Cmake (CHAR * PROG, INT Proglength) {proglength = proglength; prog = new char [proglength 1]; strcpy (prog, prog);

Cmake :: ~ cmake () {}

CToken cmake :: get_token (void) {register char * temp; ctoken m_token; m_token.token_type = 0; m_token.tok = 0; Temp = m_token.token; if (* prog == '/ 0') {* m_token. Token = '/ 0'; m_token.tok = 0; m_token.token_type = finished; return m_token;} while (iswhite (* prog)) prog; if (* prog == '/ r') // Railing {m_token.token [0] = * prog; m_token.token [1] = '/ 0'; m_token.token_type = Enter; prog ; return m_token;} if (isdelim (* prog)) // If you are found Operation symbol mark {* m_token.token = * prog; * (m_token.token 1) = '/ 0'; m_token.tok = 0; m_token.token_type = Delimiter; prog ; return m_token; //, such as token [0] = ' ' token [1] = '/ 0';} if (* prog == '") // If it is a string {PROG ; int i = 0; while (* prog! ='" '&& * PROG! = '/ r') {m_token.token [i] = * prog; i ; prog ;} PROG ; m_token.token [i] = '/ 0'; m_token.token_type = quote; return m_token;}

IF (isDigit (* prog)) // If you find a digital tag {Int i = 0; while (isdigit (* prog) && i

INT CMAKE: ISWHITE (CHAR C) {IF (c == '|| c ==' / t ') Return 1; Else Return 0;}

INT CMAKE :: ISDELIM (CHAR C) {IF (Findchar (StrDelimiter, * PROG)> = 0 || C == 9 || C == '/ r' || c == 0) Return 1; Return 0; }

INT CMAKE :: Findchar (CHAR * STR, CHAR CH) {INT Length = Strlen (Str); if (length> 0) {for (INT i = 0; I

INT cmake :: Look_UP (CHAR * C) {IF (strCMP (C, "Print") == 0) Return Print; IF (strcmp (c, "integer) == 0) Return Integer; if (strcmp (c) , "DIM") == 0) RETURN DIM; IF (strcmp (c, "as") == 0) Return AS;

Return 0;}

This is a Cexecutable package that performs code.

//// Cexecutable.h

Class Cintmember {public: char name [64]; int value;};

class CExecutable {public: CExecutable (vector tokens); virtual ~ CExecutable (); void Run (); private: vector m_tokens; file: // database means all tags vector m_intArray; file: // Pack all int type Void Run_Print (int * index); file: // * index is an important M_Tokens pointer void Run_dim (int * index); file: // ~~ void Run_assignment (int * index) ; // Assignment statement file: // Ai is auxiliary meaning, AI _ * () is an auxiliary function intloy_getnextValue (Void * Result, INT TYPE, INT * INDEX); File: // Get algebraic value Result int Ai_Getvarno (charr * name, int * result, int type; // Get the variable in array Result void get_exp (int * result, int * index); void Level2 (int * result, int * index); Void Level3 (int * * Result, int * index; void Level4 (int * result, int * index); Void Level5 (int * result, int * index); Void Level6 (int * result, int * index); Void Primitive (INT * Result) , int * index); void arch (CHAR O, INT * R, INT * H); Void SERROR (int ost); int ost_up (char * c); int Find_var (char * var_name); // Find variables, put the variable value in * value int isvar (char * name); // see if Name is a variable, return to variable type 0: What is not, 1: integer, 2: string}; /

// Executable.cpp: Implementation of the Cexecutable class.////

#include "stdafx.h" #include "executable.h"

//// construction / destruction //

Cexecutable :: Cexecutable (Vector tokens) {m_tokens = tokens;

Cexecutable :: ~ Cexecutable () {m_intarray.clear (); m_tokens.clear ();

Void Cexecutable :: Run (void) {for (int i = 0; i <= m_tokens.size () - 1; i ) // Note: i is a very important read pointer {if (m_tokens.at (i) .token_type == command) {switch (m_tokens.at (i) .tok) file: // TOK said is what command {Case Print: Run_Print (& I); Break; Case: Run_dim (& I); Break; Default: Break;}} file: // Assignment statement must be judged in the end, because if it is the condition determination after IF, you can skip the IF in the file: // front IF command (* m_tokens.at (i). Token == '=') File: // If it is assigning statement run_assignment (& i);}} void Cexecutable :: run_print (int * index) file: // * index is the pointer in m_tokens {if (* index

Void Cexecutable :: Run_DIM (int * index) file: // ~~ {INT I = * index; if (i

/ * MULTIPLY OR DIVIDE :: Level3 (int * result, int * index) {register char op; int hold; level4 (index); while ((OP = * m_tokens.at (* index ) .token == '*' || op == '/' || op == '%') {(* index) ; Level3 (& Hold, INDEX); Arith (OP, Result, & Hold); }

/ * Process Integer Exponent * / Void Cexecutable :: Level4 (INT * Result, INT * INDEX) {Register Char op; INT HOLD; Level5 (Result, INDEX); IF ((OP = * m_ToKens.at (* index). Token == '^') {(* index) ; Level5 (& Hold, INDEX); Arith (OP, Result, & Hold);}}

/ * is a unary or - * / void Cexecutable :: Level5 (int * result, int * index) {register char op; op = 0; if ((m_tokens.at (* index) .token_type == delimiter && * m_tokens.at (* index) .token == ' ' || * m_tokens.at (* index) .token == '-') {OP = * m_tokens.at (* index) .toke; (* index ;} Level6 (Result, INDEX); if (op) IF (OP == ') * Result = - (* Result);} / * process Preenthesized Expression * / Void Cexecutable: Level6 (int * Result, int * index) {IF ((* m_tokens.at (* index) .token == '(') && (m_tokens.at (* index) .token_type == delimiter) {(* index) ; Level2 (index); if (* m_tokens.at (* index) .token! = ')') SERROR (1); (* index) ;} else primitive (result, index);

/ * Find value of number or variable * / void Cexecutable :: Primitive (int * result, int * index) {int token_type = m_tokens.at (* index) .token_type; if (isvar (m_tokens.at (* index). Token_type = variable; switch (token_type) {copy variable: find_var (m_tokens.at (* index) .token, result); (* index) ; return; case number: * result = atoi (m_tokens.at (* index); (* index) ; return; default: serror (0);}}

INT CEXECUTABLE :: Find_var (CHAR * VAR_NAME, VOID * VALUE) {for (INT i = 0; i <= m_intarray.size () - 1; i ) ife (! str_name, m_intarray.at (i) .name )) {INT * INT_VALUE = (int *) value; * int_value = m_intarray.at (i) .value; return 1;} return 0;}

INT CEXECUTABLE :: isvar (char * name) {if (m_intarray.size () == 0) Return 0; for (INT i = 0; i <= m_intarray.size () - 1; i ) IF (! STRCMP) Name, m_intarray.at (i) .name)) Return Integer; return 0;} / * perform the specified arithmetic * / void Cexecutable :: Arith (Char O, INT * R, INT * H) {/ * register * / INT T, EX; Switch (o) {case '-': * r = * r- * h; Break; Case ' ': * r = * r * h; Break; Case '*': * r = * r ** h; Break; Case '/': * r = (* r) / (* h); Break; Case '%': * r = (* r)% (* H); Break; Case '^ ': EX = * r; if (* h == 0) {* r = 1; Break;} for (t = * H-1; T> 0; - t) * r = (* r) * EX ; Break;}}

INT CEXECUTABLE :: Look_UP (CHAR * C) {IF (strcmp (C, "Print") == 0) Return Print; IF (strcmp (c, "integer) == 0) Return Integer; if (strcmp (c) , "DIM") == 0) RETURN DIM; IF (strCMP (C, "AS") == 0) Return As; Return 0;}

Void Cexecutable :: SERROR (int error) {char * e [] = {"Syntax Error", "Unbalanced Preenthese", "NO Expression Present", "NOT A Variable", "Label Table Full" , "Duplicate Label", "Undefined Label", "THEN EXPECTED", "TOO MANY NESTED For Loops", "Next WITHOUT for", "Too Many Nested Gosub", "Return WISUT GOSUB"}; Printf ("% s / n", e [error]);

转载请注明原文地址:https://www.9cbs.com/read-3170.html

New Post(0)