Skip to content

Commit

Permalink
reading from console/file
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitrii-artuhov committed Jun 6, 2021
1 parent 2d3bf7e commit 0c7f313
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 14 deletions.
89 changes: 78 additions & 11 deletions gengo/gengo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,95 @@
#include "./language/context/context.h"
#include "./language/values/values.h"


int main() {
std::string mode = "";
std::cout << "Pick reading mode: file(f) or console(c)" << std::endl;

std::cin >> mode;

if (mode == "c") {
ConsoleInput();
}
else if (mode == "f") {
std::cout << "Enter path to the file:" << std::endl;
std::string path = "";

std::cin >> path; // ../../../test.gengo
FileInput(path);
}

return 0;
}

void ConsoleInput() {
std::string FILE_NAME = "<console>";
SymbolTable* global_table = new SymbolTable();
global_table->parent = nullptr;

Context* ctx = new Context(FILE_NAME, nullptr);
ctx->symbol_table = global_table;

ApplyBuiltInFunctions(ctx);

while (true) {
std::cout << "gengo > " << std::flush;
std::string line;

std::getline(std::cin, line);

if (line == "exit") {
break;
}

run(FILE_NAME, line, ctx);
}
}


void FileInput(std::string &file_name) {
std::string FILE_NAME = file_name;

// manage file
std::fstream file;
file.open(FILE_NAME, std::ios::in);

if (!file || !file.is_open()) {
std::cout << "ERROR: no file found at '" + FILE_NAME + "'" << std::endl;
return;
}

std::string text = "";
std::string line = "";

while (!file.eof()) {
std::getline(file, line);
text += line;
}


file.close();

// start programm
SymbolTable* global_table = new SymbolTable();
global_table->parent = nullptr;

Context* ctx = new Context(FILE_NAME, nullptr);
ctx->symbol_table = global_table;

ApplyBuiltInFunctions(ctx);

run(FILE_NAME, text, ctx, false);
}


void ApplyBuiltInFunctions(Context* ctx) {
/*
##########################
# BUILT_IN FUNCTIONS #
##########################
*/

// Print
ctx->symbol_table->Set(
BUILT_IN_FUNCTION_PRINT,
Expand Down Expand Up @@ -45,15 +121,6 @@ int main() {
BUILT_IN_FUNCTION_POP,
new NodeValue(ctx, BUILT_IN_FUNCTION_POP)
);


while (true) {
std::cout << "gengo > " << std::flush;
std::string line;
std::getline(std::cin, line);
run(FILE_NAME, line, ctx);
}


return 0;
}

}
11 changes: 10 additions & 1 deletion gengo/gengo.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,21 @@
#include <string>
#include <unordered_map>
#include <map>
#include <fstream>


const bool GENGO_SHOW_TOKENS = !true;
const bool GENGO_SHOW_AST = !true;
const bool GENGO_LOG_TO_CONSOLE = true;
//const bool GENGO_LOG_TO_CONSOLE = true;


const bool GENGO_PARSE = true;
const bool GENGO_INTERPRET = true;



void ConsoleInput();
void FileInput(std::string &file_name);

class Context;
void ApplyBuiltInFunctions(Context* ctx);
2 changes: 1 addition & 1 deletion gengo/language/run.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@



void run(std::string& file_name, std::string& text, Context* ctx) {
void run(std::string& file_name, std::string& text, Context* ctx, bool GENGO_LOG_TO_CONSOLE) {
// Lexer
Lexer lexer(file_name, text);
LexerResult* lex_res = lexer.MakeTokens();
Expand Down
2 changes: 1 addition & 1 deletion gengo/language/run.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@
std::vector<Token>,
IllegalCharError*
>*/
void run(std::string& file_name, std::string& text, Context* ctx);
void run(std::string& file_name, std::string& text, Context* ctx, bool GENGO_LOG_TO_CONSOLE=true);
14 changes: 14 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ arith-expr | term ((PLUS\|MINUS) term)*
term | factor ((MUL\|DIV) factor)*
factor | INT\|FLOAT\|STRING\|IDENTIFIER
&nbsp; | IDENTIFIER LPAREN (expr (COMMA expr)*)? RPAREN
&nbsp; | IDENTIFIER LSQUARE expr RSQUARE (LSQUARE expr RSQUARE)* (EQ expr)?
&nbsp; | (PLUS\|MINUS) factor
&nbsp; | LPAREN comp-expr RPAREN
&nbsp; | NOT comp-expr
&nbsp; | array-expr
&nbsp; | if-expr
&nbsp; | for-expr
&nbsp; | func-def
Expand All @@ -30,8 +32,20 @@ if-else-expr | KEYWORD:elif LPAREN expr RPAREN LBRACE statements RBRACE
&nbsp; | (if-else-expr\|else-expr)?
else-expr | KEYWORD:otherwise LBRACE statements RBRACE
for-expr | KEYWORD:loop LPAREN (expr)? NEWLINE expr NEWLINE (expr)? RPAREN LBRACE statements RBRACE
array-expr | LSQUARE (expr (COMMA expr)*)? RSQUARE



## Features (release 1.0.0)
- Console usage
- Reading from files
- Functions
- Arrays (with built-in functions: `size`, `push`, `pop`)
- Strings (with built-in functions: `length`)
- 64-bit Integers and Floats (`int`, `float`)
- General built-in functions: `print`
- Error messages

## Features (pre-release 0.6.0)
- Strings
- Built-in Functions (`print`, `size`)
Expand Down
13 changes: 13 additions & 0 deletions test.gengo
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@


int a = 10;

a = a * a - 1;

if (a > 100) {
print(a);
}
otherwise {
print("Bad a:");
print(a);
};

0 comments on commit 0c7f313

Please sign in to comment.