feat: add basic CLI (load files)
This commit is contained in:
parent
538205e8a5
commit
906949d691
11 changed files with 112 additions and 26 deletions
|
@ -3,4 +3,4 @@ set a to input_number()
|
||||||
set b to input_number()
|
set b to input_number()
|
||||||
set res to a+b
|
set res to a+b
|
||||||
|
|
||||||
eval print_int(c)
|
print_int(res)
|
||||||
|
|
|
@ -1,2 +1,3 @@
|
||||||
# just print 42
|
# just print 42
|
||||||
eval print_number(42)
|
print_number(42)
|
||||||
|
|
||||||
|
|
5
examples/sequence.ltor
Normal file
5
examples/sequence.ltor
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
set i to 0
|
||||||
|
while !(i = 10) do
|
||||||
|
print_number(i)
|
||||||
|
set i to i+1
|
||||||
|
end
|
6
src/config.h
Normal file
6
src/config.h
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
#ifndef G_CONFIG_H_
|
||||||
|
#define G_CONFIG_H_
|
||||||
|
|
||||||
|
#define G_DEBUG_LEVEL 0
|
||||||
|
|
||||||
|
#endif
|
|
@ -539,7 +539,7 @@ int evaluate(struct StateContainer* state, char* inputStr, int* resultPtr, unsig
|
||||||
// identify token
|
// identify token
|
||||||
// first try a variable then a func name
|
// first try a variable then a func name
|
||||||
// not a float, check if this is a common function name
|
// not a float, check if this is a common function name
|
||||||
if (EVALUATOR_DEBUG_LEVEL >= 2) printf("now going to identify token '%s' \n");
|
if (EVALUATOR_DEBUG_LEVEL >= 2) printf("now going to identify token '%s' \n", buff);
|
||||||
if (EVALUATOR_DEBUG_LEVEL >= 2) var_store_print(state->varStore);
|
if (EVALUATOR_DEBUG_LEVEL >= 2) var_store_print(state->varStore);
|
||||||
|
|
||||||
int varKey = (int) var_store_get_key(state->varStore, buff);
|
int varKey = (int) var_store_get_key(state->varStore, buff);
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include "./config.h"
|
||||||
#include "./types.h"
|
#include "./types.h"
|
||||||
#include "./utils.h"
|
#include "./utils.h"
|
||||||
|
|
||||||
|
@ -246,7 +247,7 @@ int execute_func(short funcID, short argsLen, unsigned char* argsTypes, int* arg
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
char* name = intros[funcID].name;
|
char* name = intros[funcID].name;
|
||||||
printf("Executing func '%s' \n", name);
|
if (G_DEBUG_LEVEL >= 2) printf("Executing func '%s' \n", name);
|
||||||
if (argsLen < intros[funcID].nbArgs) {
|
if (argsLen < intros[funcID].nbArgs) {
|
||||||
printf("ERR: Too few arguments for func call '%s' \n", name);
|
printf("ERR: Too few arguments for func call '%s' \n", name);
|
||||||
return 1;
|
return 1;
|
||||||
|
@ -260,7 +261,7 @@ int execute_func(short funcID, short argsLen, unsigned char* argsTypes, int* arg
|
||||||
// first cast the function ptr
|
// first cast the function ptr
|
||||||
impl(resPtr, resTypePtr, argsTypes, argsValues);
|
impl(resPtr, resTypePtr, argsTypes, argsValues);
|
||||||
|
|
||||||
printf("Got %s \n", get_repr(*resTypePtr, resPtr));
|
if (G_DEBUG_LEVEL >= 2) printf("Got %s \n", get_repr(*resTypePtr, resPtr));
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
@ -432,7 +432,7 @@ int process_line(struct StateContainer* state, char* str)
|
||||||
for (int z = 0; z < nameLen; z++) {
|
for (int z = 0; z < nameLen; z++) {
|
||||||
name[z] = str[setStatementParsing.name_start + z];
|
name[z] = str[setStatementParsing.name_start + z];
|
||||||
}
|
}
|
||||||
name[len] = '\0';
|
name[nameLen] = '\0';
|
||||||
|
|
||||||
int res;
|
int res;
|
||||||
byte resType;
|
byte resType;
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
#ifndef LINE_PROCESSING_H_
|
#ifndef LINE_PROCESSING_H_
|
||||||
#define LINE_PROCESSING_H_
|
#define LINE_PROCESSING_H_
|
||||||
|
|
||||||
#define LINE_PROCESSING_DEBUG_LEVEL 1
|
#define LINE_PROCESSING_DEBUG_LEVEL 0
|
||||||
|
|
||||||
#define BLOCK_IF 2
|
#define BLOCK_IF 2
|
||||||
#define BLOCK_WHILE 3
|
#define BLOCK_WHILE 3
|
||||||
|
|
107
src/main.c
107
src/main.c
|
@ -1,45 +1,114 @@
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include "./config.h"
|
||||||
|
#include "./types.h"
|
||||||
|
#include "./utils.h"
|
||||||
#include "./stack.h"
|
#include "./stack.h"
|
||||||
#include "./list.h"
|
#include "./list.h"
|
||||||
#include "./number_parsing.h"
|
#include "./number_parsing.h"
|
||||||
#include "./evaluator.h"
|
#include "./evaluator.h"
|
||||||
#include "./utils.h"
|
#include "./state.h"
|
||||||
#include "./types.h"
|
#include "./var_store.h"
|
||||||
#include "./line_processing.h"
|
#include "./line_processing.h"
|
||||||
|
|
||||||
byte load_file(char* filename, char** resultPtr) {
|
#define HELP_FLAG 1
|
||||||
FILE* f = fopen(filename, "rb");
|
#define VERSION_FLAG 4
|
||||||
|
#define INTERACTIVE_FLAG 8
|
||||||
|
#define STDIN_EXP_FLAG 16
|
||||||
|
|
||||||
|
int help_mode() {
|
||||||
|
char* helpStr = "Usage: langatator [options] [file]\n"
|
||||||
|
" -h --help print this usage and exit\n"
|
||||||
|
" -v --version print version information and exit\n"
|
||||||
|
" -i --interactive force interactive mode\n"
|
||||||
|
" -e --stdin-expression evaluate expression from stdin\n";
|
||||||
|
printf(helpStr);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int version_mode() {
|
||||||
|
char* helpStr = "Langatator 0.0.1\n";
|
||||||
|
printf(helpStr);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int interactive_mode() {
|
||||||
|
printf("Interactive not implemented yet ¯\\_(ツ)_/¯ \n");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int stdin_expression_mode() {
|
||||||
|
printf("This mode is not implemented yet ¯\\_(ツ)_/¯ \n");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int file_mode(char* fileName) {
|
||||||
|
FILE* f = fopen(fileName, "rb");
|
||||||
|
|
||||||
if (!f) {
|
if (!f) {
|
||||||
return 0;
|
printf(stderr, "Cannot load file '%s' status: %d\n", fileName, f);
|
||||||
|
return 1;
|
||||||
}
|
}
|
||||||
fseek(f, 0, SEEK_END);
|
fseek(f, 0, SEEK_END);
|
||||||
long length = ftell(f);
|
long length = ftell(f);
|
||||||
fseek (f, 0, SEEK_SET);
|
fseek(f, 0, SEEK_SET);
|
||||||
char* buffer = (char*) malloc(length);
|
|
||||||
if (!buffer) {
|
char* buff = (char*) malloc(length);
|
||||||
return 0;
|
if (!buff) {
|
||||||
|
printf(stderr, "Could not allocate buffer to process file content\n");
|
||||||
|
return 1;
|
||||||
}
|
}
|
||||||
if (buffer) {
|
if (buff) {
|
||||||
fread(buffer, 1, length, f);
|
fread(buff, 1, length, f);
|
||||||
}
|
}
|
||||||
fclose(f);
|
fclose(f);
|
||||||
|
|
||||||
*resultPtr = buffer;
|
struct StateContainer* state = state_init();
|
||||||
|
process_script(state, buff);
|
||||||
|
|
||||||
return 1;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int main (int argc, char** argv) {
|
int main (int argc, char** argv) {
|
||||||
// if (argc == 1) {
|
int flags = INTERACTIVE_FLAG;
|
||||||
// printf("One arg '%s'", argv[0]);
|
|
||||||
|
|
||||||
// return 0;
|
int nonFlagArgumentCount = 0;
|
||||||
// }
|
int nonFlagArgumentPos = 0;
|
||||||
|
|
||||||
// printf("Invalid usage use --help to document your self\n");
|
char* cmdName = argv[0];
|
||||||
printf("CLI not implemented yet ¯\\_(ツ)_/¯ \n");
|
for (int i = 1; i < argc; i++) {
|
||||||
|
if (str_starts_with("--help", argv[i]) || str_starts_with("-h", argv[i])) {
|
||||||
|
flags = HELP_FLAG;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (str_starts_with("--version", argv[i]) || str_starts_with("-v", argv[i])) {
|
||||||
|
flags = VERSION_FLAG;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (str_starts_with("--interactive", argv[i]) || str_starts_with("-i", argv[i])) {
|
||||||
|
flags = INTERACTIVE_FLAG;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (str_starts_with("--stdin-expression", argv[i]) || str_starts_with("-e", argv[i])) {
|
||||||
|
flags = STDIN_EXP_FLAG;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
// no flag match
|
||||||
|
if (str_starts_with("--", argv[i])) {
|
||||||
|
printf(stderr, "Invalid flag '%s'\n", argv[i]);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
nonFlagArgumentCount++;
|
||||||
|
nonFlagArgumentPos = i;
|
||||||
|
}
|
||||||
|
if (nonFlagArgumentCount == 1) return file_mode(argv[nonFlagArgumentPos]);
|
||||||
|
|
||||||
|
if (flags & HELP_FLAG) return help_mode();
|
||||||
|
if (flags & VERSION_FLAG) return version_mode();
|
||||||
|
if (flags & INTERACTIVE_FLAG) return interactive_mode();
|
||||||
|
if (flags & STDIN_EXP_FLAG) return stdin_expression_mode();
|
||||||
|
|
||||||
|
help_mode();
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
#include "./config.h"
|
||||||
|
|
||||||
#ifndef UTILS_H_
|
#ifndef UTILS_H_
|
||||||
#define UTILS_H_
|
#define UTILS_H_
|
||||||
|
|
||||||
|
|
|
@ -61,4 +61,6 @@ byte var_store_set(struct VariableStore* store, char* varName, byte type, void*
|
||||||
|
|
||||||
byte var_store_delete(struct VariableStore* store, char* varName);
|
byte var_store_delete(struct VariableStore* store, char* varName);
|
||||||
|
|
||||||
|
void var_store_print(struct VariableStore* store);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Reference in a new issue