Posts

Showing posts from November, 2023

tape_t.h

 #pragma once #include <stdlib.h> #include <stdio.h> enum Bool { FALSE, TRUE }; char getch(char *Tape, int current_ptr) { return Tape[current_ptr]; } char getchIncr(char *Tape, int current_ptr) { return Tape[current_ptr++]; } int tapeIncr(int current_ptr, int i) { current_ptr += i; return current_ptr; } void necessary_expect(char *Tape, int current_ptr, char expected_symbol) { if (getch(Tape, current_ptr) != expected_symbol) { printf("Missing char is %c but input char is (%c)%d\n", expected_symbol, getch(Tape, current_ptr), getch(Tape, current_ptr)); exit(0); } current_ptr++; } Bool optional_expect(char *Tape, int current_ptr, char expected_symbol) { if (getch(Tape, current_ptr) != expected_symbol) { return FALSE; } current_ptr++; return TRUE; } void jumpline(char *Tape, int current_ptr) { if (getch(Tape, current_ptr) == '\n') { current_ptr++; return; } while (getch(Tape, current_ptr) != '\n') ...

string.h

#pragma once #include <stdio.h> #include <stdlib.h> #include <cstring> #undef empty #define STR_LEN_CHECK(str,pos)  str->len <= pos #define IS_SPACE(str,pos) at(str,pos) == ' ' ||  \ at(str, pos) == '\n' || \ at(str, pos) == '\v' || \ at(str, pos) == '\t' struct string_struct { int len; char *str; char *back; char empty; }; typedef struct string_struct struct_var; typedef struct_var* string_t; typedef struct mem { string_t str; int string_size; int index; } memrecord; string_t string_file(char *s,int *len); string_t string(char *s); void push(string_t st, char s); char pop(string_t st); char *con(string_t a); int length(string_t a); char at(string_t a, int i); string_t copy(string_t a, string_t b); string_t getlines(string_t a); int locate(string_t s, string_t find_str); string_t partOfstring(string_t s, int start, int length); char* c_string(string_t s); void manipulate(string_t s, const char *str, int start);...

string_t.cpp

 #include "stdafx.h" #include "string_t.h" int string_file_len(char *s) { int i = 0; while (s[i] != EOF) { i++; } return i; } string_t string_file(char *s,int *file_length) { int len = string_file_len(s); char *content = (char *)malloc(len + 1); memcpy(content, s, len); content[len] = EOF; string_t st = (string_t)malloc(sizeof(struct_var)); st->len = len + 1; st->str = content; st->back = content + len - 1; if (strcmp(s, "\0")) st->empty = 1;     *file_length = len; return st; }  string_t string(char *s) { int len = strlen(s); char *content = (char *)malloc(len + 1); memcpy(content, s, len); content[len] = '\0'; string_t st = (string_t)malloc(sizeof(struct_var)); st->len = len + 1; st->str = content; st->back = content + len - 1; if (strcmp(s, "\0")) st->empty = 1; return st; } #define empty_str(st)   if(st->len == 0)              ...

Lexer_in_C

 // Lexer_in_C.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include "tape_t.h" #include "string_t.h" #include "Lexer.h" void debug_lex(int token) { switch (token) {                  case '(':         printf("(\n");      break;     case ')':         printf(")\n");         break;     case '{':         printf("{\n");         break;     case '}':         printf("}\n");         break;     case '\'':         printf("\'\n");         break;     case '"':         printf("\"\n");         break;     case ';':         printf(";\n");         break; case ELIPPSIS: printf("ELIP...

Lexer.h

#include "string_t.h" #include <signal.h> #define CRASH_NOW()  raise(SIGSEGV);  #define EOT -9999 #define Is_EndOfFile(str,pos) at(str,pos) == EOF /*#define NL_CHECK(pos)     if (NULL_CHECK(pos))    \   {                  \   pos = 0;         \   line_no++;       \   }   */ //#define ALPHA_NUM_CHECK(pos) ((CHAR_AT(pos, line_no) >= 'a' && CHAR_AT(pos, line_no) <= 'z') || (CHAR_AT(pos, line_no) >= 'A' && CHAR_AT(pos, line_no) <= 'Z') || (CHAR_AT(pos, line_no) >= '0' && CHAR_AT(pos, line_no) <= '9')) /* Macro Values for Tokens */ #define ELIPPSIS 0x1000 #define ADD_ASSIGN 0x1001 #define INC_OP 0x1002 #define SUB_ASSIGN 0x1003 #define DEC_OP 0x1004 #define DIV_ASSIGN 0x1005 #define MUL_ASSIGN 0x1006 #define MOD_ASSIGN 0x1007 #define AND_ASSIGN 0x1008 #define X...

Lexer.c

#include "stdafx.h" #include "Lexer.h" //#include "string_t.h" #include <iostream> int keyword_check(string_t str, bool *is_keyword) { char* keyword[32] = { "auto", "break", "case", "char", "const", "continue", "default", "do", "double", "else", "enum", "extern", "float", "for", "goto", "if", "int", "long", "register", "return", "short", "signed", "sizeof", "static", "struct", "switch", "typedef", "union", "unsigned", "void", "volatile", "while" }; for (int i = 0; i < 32; i++) { string_t key = string(keyword[i]); if (compare(str, key)== 0) { *is_keywor...