-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added python demo. Shifted gears to c++.
- Loading branch information
Showing
29 changed files
with
8,531 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,3 +36,4 @@ solution "LemonWreck" | |
--buildoptions { "-std=gnu++0x" } | ||
|
||
include "../src/lwcalc" | ||
include "../src/lwpython" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
def test(): | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#include "ast.h" | ||
#include "malloc.h" | ||
|
||
ast_node* create_literal_ast(scanner_token *token) | ||
{ | ||
ast_node* node = malloc(sizeof(ast_node)); | ||
node->kind = AST_LITERAL; | ||
node->token = token; | ||
return node; | ||
} | ||
ast_node* create_binary_ast(scanner_token *token, ast_node* left, ast_node* right) | ||
{ | ||
ast_node* node = malloc(sizeof(ast_node)); | ||
node->kind = AST_BINARY; | ||
node->token = token; | ||
node->node.binary.left = left; | ||
node->node.binary.right = right; | ||
return node; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#ifndef _AST_H | ||
#define _AST_H | ||
|
||
#include "token.h" | ||
|
||
enum ast_kind { | ||
AST_LITERAL, | ||
AST_BINARY | ||
}; | ||
typedef struct _ast_node { | ||
enum ast_kind kind; | ||
scanner_token *token; | ||
union { | ||
struct { | ||
struct _ast_node *left; | ||
struct _ast_node *right; | ||
} binary; | ||
} node ; | ||
} ast_node; | ||
|
||
ast_node* create_literal_ast(scanner_token *token); | ||
ast_node* create_binary_ast(scanner_token *token, ast_node* left, ast_node* right); | ||
|
||
#endif //_AST_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <malloc.h> | ||
|
||
#include "token.h" | ||
#include "compiler.h" | ||
#include "Lexer.h" | ||
#include "parser.h" | ||
|
||
int compiler::compile_file(char* s) | ||
{ | ||
scanner_token *token; | ||
Lexer *state; | ||
void* pParser = ParseAlloc(malloc); | ||
|
||
clear(); | ||
|
||
ParseTrace(stdout, ">>"); | ||
|
||
state = new Lexer(); | ||
state->init(s); | ||
|
||
for(;;) { | ||
token = state->scan(); | ||
if(token == NULL) break; | ||
Parse(pParser, token->kind, token, this); | ||
} | ||
|
||
Parse(pParser, 0, 0, this); | ||
ParseFree(pParser, free); | ||
|
||
if(errormsg != NULL) | ||
return 0; | ||
else | ||
return 1; | ||
} | ||
void compiler::clear() | ||
{ | ||
errormsg = NULL; | ||
} | ||
void compiler::error(char *errmsg) | ||
{ | ||
errormsg = errmsg; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#ifndef _COMPILER_H | ||
#define _COMPILER_H | ||
|
||
#include "ast.h" | ||
|
||
struct compiler { | ||
ast_node *ast; | ||
char* errormsg; | ||
// | ||
void clear(); | ||
int compile_file(char* s); | ||
void error(char *errmsg); | ||
} ; | ||
|
||
#endif //_COMPILER_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
#include "Lexer.h" | ||
#include <stdio.h> | ||
#include <malloc.h> | ||
|
||
#define BSIZE BUFSIZ | ||
|
||
void Lexer::init(const char *filename) | ||
{ | ||
this->file = fopen(filename, "r" ); | ||
|
||
this->line = 0; | ||
this->column = 0; | ||
|
||
this->eof = 0; | ||
this->bot = 0; | ||
this->top = 0; | ||
this->cursor = 0; | ||
this->ccursor = 0; | ||
this->lim = 0; | ||
} | ||
|
||
void Lexer::fill() | ||
{ | ||
if(!this->eof) { | ||
int cnt = this->ccursor - this->bot; | ||
if(cnt){ | ||
memcpy(this->bot, this->ccursor, this->lim - this->ccursor); | ||
this->ccursor = this->bot; | ||
this->ptr -= cnt; | ||
this->cursor -= cnt; | ||
this->pos -= cnt; | ||
this->lim -= cnt; | ||
} | ||
if((this->top - this->lim) < BSIZE){ | ||
scanchar_t *buf = (scanchar_t*) malloc(((this->lim - this->bot) + BSIZE)*sizeof(scanchar_t)); | ||
memcpy(buf, this->ccursor, this->lim - this->ccursor); | ||
this->ccursor = buf; | ||
this->ptr = &buf[this->ptr - this->bot]; | ||
this->cursor = &buf[this->cursor - this->bot]; | ||
this->pos = &buf[this->pos - this->bot]; | ||
this->lim = &buf[this->lim - this->bot]; | ||
this->top = &this->lim[BSIZE]; | ||
free(this->bot); | ||
this->bot = buf; | ||
} | ||
if((cnt = fread((char*) this->lim, sizeof(scanchar_t), BSIZE, this->file)) != BSIZE){ | ||
this->eof = &this->lim[cnt]; *(this->eof)++ = '\0'; | ||
} | ||
this->lim += cnt; | ||
} | ||
} | ||
scanner_token* Lexer::create_token(int kind) { | ||
scanner_token* token = (scanner_token*)malloc(sizeof(scanner_token)); | ||
token->kind = kind; | ||
token->line = this->line; | ||
token->column = this->column; | ||
return token; | ||
} | ||
scanner_token* Lexer::emit_newline() { | ||
scanner_token* token = create_token(TOKEN_NEWLINE); | ||
this->pos = this->cursor; | ||
this->line++; | ||
return token; | ||
} | ||
scanner_token* Lexer::create_name_token(char* value) { | ||
scanner_token* token = create_token(TOKEN_NAME); | ||
token->data.str = value; | ||
return token; | ||
} | ||
scanner_token* Lexer::create_int_token(int value) { | ||
scanner_token* token = create_token(TOKEN_INTEGER); | ||
token->data.num = value; | ||
return token; | ||
} | ||
scanner_token* Lexer::create_float_token(float value) { | ||
scanner_token* token = create_token(TOKEN_FLOAT); | ||
token->data.num = value; | ||
return token; | ||
} | ||
void Lexer::capture_begin() | ||
{ | ||
this->ccursor = this->cursor; | ||
this->column = this->ccursor - this->pos; | ||
} | ||
void Lexer::capture() | ||
{ | ||
size_t len = this->cursor - this->ccursor; | ||
memcpy(this->cbuffer, this->ccursor, len); | ||
this->cbuffer[len] = '\0'; | ||
//capture_begin(); | ||
} | ||
scanchar_t *Lexer::capture_string() | ||
{ | ||
size_t len = this->cursor - this->ccursor; | ||
scanchar_t* str = (scanchar_t*)malloc(len + 1); | ||
memcpy(str, this->ccursor, len); | ||
str[len] = '\0'; | ||
//capture_begin(); | ||
return str; | ||
} | ||
int Lexer::capture_int() | ||
{ | ||
capture(); | ||
return atoi(this->cbuffer); | ||
} | ||
float Lexer::capture_float() | ||
{ | ||
capture(); | ||
return (float)atof(this->cbuffer); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
#ifndef _SCANNER_H | ||
#define _SCANNER_H | ||
|
||
#include <assert.h> | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
|
||
#include "token.h" | ||
|
||
typedef char scanchar_t; | ||
|
||
class Lexer { | ||
public: | ||
//int fd; //file descriptor | ||
FILE *file; | ||
int line; | ||
int column; | ||
// | ||
scanchar_t buffer[BUFSIZ]; //fill buffer | ||
scanchar_t cbuffer[BUFSIZ]; //capture buffer | ||
scanchar_t* cursor; | ||
scanchar_t* ccursor; | ||
scanchar_t* bot; | ||
scanchar_t* lim; | ||
scanchar_t* ptr; | ||
scanchar_t* pos; | ||
scanchar_t* top; | ||
scanchar_t* marker; | ||
scanchar_t* eof; | ||
// | ||
void init(const char *filename); | ||
scanner_token *scan(); | ||
void fill(); | ||
|
||
scanner_token* emit_newline(); | ||
|
||
scanner_token* create_token(int kind); | ||
scanner_token* create_name_token(char* value); | ||
scanner_token* create_int_token(int value); | ||
scanner_token* create_float_token(float value); | ||
|
||
void capture_begin(); | ||
void capture(); | ||
scanchar_t *capture_string(); | ||
int capture_int(); | ||
float capture_float(); | ||
}; | ||
|
||
#endif |
Oops, something went wrong.