Compare commits
28 Commits
9a9e5cd3e0
...
v0.1
Author | SHA1 | Date | |
---|---|---|---|
694eb43eab | |||
694d40124f | |||
39778ce08d | |||
cfd44621d5 | |||
4be71317b0 | |||
49642553e1 | |||
1e11b5921d | |||
7343c3b9d9 | |||
9e8410d4cf | |||
bc0c4f33ad | |||
1098fa252f | |||
0b1905429c | |||
e7d3ea3697 | |||
577bde6e57 | |||
60b9ed9eb2 | |||
d13bf883b5 | |||
681e005a68 | |||
2ce89fb39a | |||
907bc26264 | |||
e243e862ae | |||
5e930b9847 | |||
ed3ec885c0 | |||
835bcfe121 | |||
9432496875 | |||
0731e40e6a | |||
35322de3ac | |||
c3d8d6f8e5 | |||
1f2bca5028 |
33
Makefile
33
Makefile
@@ -15,6 +15,9 @@ CC = clang
|
|||||||
LINK = clang
|
LINK = clang
|
||||||
CFLAGS = -Wall -DDBG -ggdb
|
CFLAGS = -Wall -DDBG -ggdb
|
||||||
LDFLAGS = -lm
|
LDFLAGS = -lm
|
||||||
|
BATS = bats
|
||||||
|
BISON = bison
|
||||||
|
PRINT = echo -e
|
||||||
|
|
||||||
SRC_FILES = $(wildcard $(SRC_DIR)/*.c)
|
SRC_FILES = $(wildcard $(SRC_DIR)/*.c)
|
||||||
OBJ_FILES = $(patsubst $(SRC_DIR)/%.c, $(OBJ_DIR)/%.o, $(SRC_FILES))
|
OBJ_FILES = $(patsubst $(SRC_DIR)/%.c, $(OBJ_DIR)/%.o, $(SRC_FILES))
|
||||||
@@ -27,6 +30,7 @@ UNITY_OBJ = $(TEST_BUILD_DIR)/unity.o
|
|||||||
TEST_SRC_FILES = $(wildcard $(TEST_DIR)/*.c)
|
TEST_SRC_FILES = $(wildcard $(TEST_DIR)/*.c)
|
||||||
TEST_OBJ_FILES = $(patsubst $(TEST_DIR)/%.c, $(TEST_OBJ_DIR)/%.o, $(TEST_SRC_FILES))
|
TEST_OBJ_FILES = $(patsubst $(TEST_DIR)/%.c, $(TEST_OBJ_DIR)/%.o, $(TEST_SRC_FILES))
|
||||||
TEST_BIN_FILES = $(patsubst $(TEST_DIR)/%.c, $(TEST_BUILD_DIR)/%.out, $(TEST_SRC_FILES))
|
TEST_BIN_FILES = $(patsubst $(TEST_DIR)/%.c, $(TEST_BUILD_DIR)/%.out, $(TEST_SRC_FILES))
|
||||||
|
TEST_VAL_DIR = $(TEST_DIR)/validation
|
||||||
|
|
||||||
RESETCOLOR = \033[0m
|
RESETCOLOR = \033[0m
|
||||||
WHITE = $(RESETCOLOR)\033[37m
|
WHITE = $(RESETCOLOR)\033[37m
|
||||||
@@ -45,53 +49,56 @@ run: $(TARGET)
|
|||||||
# Generate grammars with bison.
|
# Generate grammars with bison.
|
||||||
$(GRAM_FILES): $(SRC_DIR)/grammar.y
|
$(GRAM_FILES): $(SRC_DIR)/grammar.y
|
||||||
@ mkdir -p $(GRAM_DIR)
|
@ mkdir -p $(GRAM_DIR)
|
||||||
@ echo -e "$(WHITE_BOLD)Generating grammars...$(RESETCOLOR)"
|
@ $(PRINT) "$(WHITE_BOLD)Generating grammars...$(RESETCOLOR)"
|
||||||
bison $< -o$(GRAM_DIR)/grammar.tab.c -H$(GRAM_DIR)/grammar.tab.h
|
$(BISON) $< -o$(GRAM_DIR)/grammar.tab.c -H$(GRAM_DIR)/grammar.tab.h
|
||||||
|
|
||||||
# Compile grammars.
|
# Compile grammars.
|
||||||
$(OBJ_DIR)/grammar.o: $(GRAM_DIR)/grammar.tab.c $(GRAM_DIR)/grammar.tab.h $(OBJ_DIR)/lexer.o
|
$(OBJ_DIR)/grammar.o: $(GRAM_DIR)/grammar.tab.c $(GRAM_DIR)/grammar.tab.h $(OBJ_DIR)/lexer.o
|
||||||
@ echo -e "$(WHITE_BOLD)Compiling grammars...$(RESETCOLOR)"
|
@ $(PRINT) "$(WHITE_BOLD)Compiling grammars...$(RESETCOLOR)"
|
||||||
$(CC) $(CFLAGS) -c $< -o $@
|
$(CC) $(CFLAGS) -c $< -o $@
|
||||||
|
|
||||||
# Lexer depends on grammars.
|
# Lexer depends on grammars.
|
||||||
$(OBJ_DIR)/lexer.o: $(SRC_DIR)/lexer.c $(GRAM_FILES)
|
$(OBJ_DIR)/lexer.o: $(SRC_DIR)/lexer.c $(GRAM_FILES)
|
||||||
@ mkdir -p $(OBJ_DIR)
|
@ mkdir -p $(OBJ_DIR)
|
||||||
@ echo -e "$(WHITE_BOLD)Compiling source object $(WHITE)$@$(WHITE_BOLD)... $(RESETCOLOR)"
|
@ $(PRINT) "$(WHITE_BOLD)Compiling source object $(WHITE)$@$(WHITE_BOLD)... $(RESETCOLOR)"
|
||||||
$(CC) $(CFLAGS) -c $< -o $@
|
$(CC) $(CFLAGS) -c $< -o $@
|
||||||
|
|
||||||
# Compile project source objects.
|
# Compile project source objects.
|
||||||
$(OBJ_DIR)/%.o: $(SRC_DIR)/%.c $(INC_DIR)/%.h
|
$(OBJ_DIR)/%.o: $(SRC_DIR)/%.c $(INC_DIR)/%.h
|
||||||
@ mkdir -p $(OBJ_DIR)
|
@ mkdir -p $(OBJ_DIR)
|
||||||
@ echo -e "$(WHITE_BOLD)Compiling source object $(WHITE)$@$(WHITE_BOLD)... $(RESETCOLOR)"
|
@ $(PRINT) "$(WHITE_BOLD)Compiling source object $(WHITE)$@$(WHITE_BOLD)... $(RESETCOLOR)"
|
||||||
$(CC) $(CFLAGS) -c $< -o $@
|
$(CC) $(CFLAGS) -c $< -o $@
|
||||||
|
|
||||||
# Link to final binary.
|
# Link to final binary.
|
||||||
$(TARGET): $(OBJ_DIR)/grammar.o $(OBJ_FILES)
|
$(TARGET): $(OBJ_DIR)/grammar.o $(OBJ_FILES)
|
||||||
@ echo -e "$(WHITE_BOLD)Linking $(WHITE)$@$(WHITE_BOLD)...$(RESETCOLOR)"
|
@ $(PRINT) "$(WHITE_BOLD)Linking $(WHITE)$@$(WHITE_BOLD)...$(RESETCOLOR)"
|
||||||
$(LINK) -o $(TARGET) $(OBJ_FILES) $(OBJ_DIR)/grammar.o $(LDFLAGS)
|
$(LINK) -o $(TARGET) $(OBJ_FILES) $(OBJ_DIR)/grammar.o $(LDFLAGS)
|
||||||
|
|
||||||
# Compile Unity object.
|
# Compile Unity object.
|
||||||
$(UNITY_OBJ): $(UNITY_C) $(UNITY_H)
|
$(UNITY_OBJ): $(UNITY_C) $(UNITY_H)
|
||||||
@ echo -e "$(WHITE_BOLD)Compiling Unity...$(RESETCOLOR)"
|
@ $(PRINT) "$(WHITE_BOLD)Compiling Unity...$(RESETCOLOR)"
|
||||||
$(CC) $(CFLAGS) -D UNITY_OUTPUT_COLOR -c $< -o $@
|
$(CC) $(CFLAGS) -D UNITY_OUTPUT_COLOR -c $< -o $@
|
||||||
|
|
||||||
# Compile test object.
|
# Compile test object.
|
||||||
$(TEST_OBJ_DIR)/test_%.o: $(TEST_DIR)/test_%.c
|
$(TEST_OBJ_DIR)/test_%.o: $(TEST_DIR)/test_%.c
|
||||||
@ echo -e "$(WHITE_BOLD)Compiling test object $(WHITE)$@$(WHITE_BOLD)...$(RESETCOLOR)"
|
@ $(PRINT) "$(WHITE_BOLD)Compiling test object $(WHITE)$@$(WHITE_BOLD)...$(RESETCOLOR)"
|
||||||
$(CC) $(CFLAGS) -c $< -o $@
|
$(CC) $(CFLAGS) -c $< -o $@
|
||||||
|
|
||||||
# Link final test binary.
|
# Link final test binary.
|
||||||
$(TEST_BUILD_DIR)/test_%.out: $(TEST_OBJ_DIR)/test_%.o $(OBJ_DIR)/%.o $(UNITY_OBJ)
|
$(TEST_BUILD_DIR)/test_%.out: $(TEST_OBJ_DIR)/test_%.o $(OBJ_DIR)/%.o $(UNITY_OBJ)
|
||||||
@ echo -e "$(WHITE_BOLD)Linking test binary $(WHITE)$@$(WHITE_BOLD)...$(RESETCOLOR)"
|
@ $(PRINT) "$(WHITE_BOLD)Linking test binary $(WHITE)$@$(WHITE_BOLD)...$(RESETCOLOR)"
|
||||||
$(LINK) -o $@ $? $(LDFLAGS)
|
$(LINK) -o $@ $? $(LDFLAGS)
|
||||||
|
|
||||||
# Run the test files.
|
# Run the test files.
|
||||||
test: $(TEST_BIN_FILES)
|
test: $(TARGET) $(TEST_BIN_FILES)
|
||||||
@ echo -e "$(WHITE_BOLD)Running tests...$(RESETCOLOR)"
|
@ $(PRINT) "$(WHITE_BOLD)Running unit tests...$(RESETCOLOR)"
|
||||||
for test in $<; do ./$${test}; done
|
for test in $(TEST_BIN_FILES); do ./$${test}; done
|
||||||
|
@ $(PRINT) "$(WHITE_BOLD)Running validation tests...$(RESETCOLOR)"
|
||||||
|
$(BATS) $(TEST_VAL_DIR)
|
||||||
|
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
@ echo -e "$(WHITE_BOLD)Cleaning up...$(RESETCOLOR)"
|
@ $(PRINT) "$(WHITE_BOLD)Cleaning up...$(RESETCOLOR)"
|
||||||
rm -rf $(OBJ_DIR)/*.o $(TEST_OBJ_DIR)/*.o $(TEST_BUILD_DIR)/test.out $(TARGET) $(GRAM_DIR)/* $(UNITY_OBJ)
|
rm -rf $(OBJ_DIR)/*.o $(TEST_OBJ_DIR)/*.o $(TEST_BUILD_DIR)/test.out $(TARGET) $(GRAM_DIR)/* $(UNITY_OBJ)
|
||||||
|
|
||||||
.PHONY: all clean test nocolor release run
|
.PHONY: all clean test nocolor release run
|
||||||
|
12
README.md
12
README.md
@@ -1,5 +1,7 @@
|
|||||||
# SCL: Simple CAS Language
|
# SCL: Simple CAS Language
|
||||||
|
|
||||||
|
Version v1.0-alpha
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
@@ -8,13 +10,13 @@ make release # Build.
|
|||||||
./scl # Run.
|
./scl # Run.
|
||||||
```
|
```
|
||||||
|
|
||||||
|
If you wish to run tests, make sure to run `git clone --recurse-submodules` to
|
||||||
|
include the [Unity](https://github.com/ThrowTheSwitch/Unity) test framework.
|
||||||
|
|
||||||
## Current State
|
## Current State
|
||||||
|
|
||||||
The following things are possible:
|
See [STATUS.md](STATUS.md). Currently, one is able to use `scl` as a basic,
|
||||||
1. Floating-point numbers
|
interactive, four-function calculator.
|
||||||
2. Negative numbers
|
|
||||||
3. Basic binary infix operators; `+`, `-`, `*`, `/`
|
|
||||||
4. The same as conventional functions; `sum()`, `sub()`, `mul()`, `div()`
|
|
||||||
|
|
||||||
## Syntax (Planned)
|
## Syntax (Planned)
|
||||||
|
|
||||||
|
55
STATUS.md
Normal file
55
STATUS.md
Normal file
@@ -0,0 +1,55 @@
|
|||||||
|
# SCL Design Status
|
||||||
|
|
||||||
|
- [x] Data definitions
|
||||||
|
- [x] Token Definitions
|
||||||
|
- [x] AST Definitions
|
||||||
|
|
||||||
|
- [ ] Parser
|
||||||
|
- [x] Parse numbers
|
||||||
|
- [x] Parse floats
|
||||||
|
- [x] Parse negative numbers
|
||||||
|
- [x] Parse infix operators
|
||||||
|
- [x] Order of operations
|
||||||
|
- [x] Parse function application
|
||||||
|
- [x] Parse order of operations with parenthesis
|
||||||
|
- [ ] Parse variable invocation
|
||||||
|
- [ ] Parse variable definition
|
||||||
|
- [ ] Parse types
|
||||||
|
- [ ] Parse function definition
|
||||||
|
- [ ] Parse lists/arrays/vectors
|
||||||
|
- [ ] Parse blocks
|
||||||
|
- [ ] Parse control flow
|
||||||
|
- [ ] Parse `if` statements
|
||||||
|
- [ ] Parse `loop`s
|
||||||
|
- [ ] Parse `for` loops
|
||||||
|
- [ ] Parse `while` loops
|
||||||
|
- [ ] Parse `case` statements
|
||||||
|
- [ ] Parse `goto` statements
|
||||||
|
- [ ] Parse lambda function definition
|
||||||
|
- [ ] Parse function calling with positional arguments
|
||||||
|
- [ ] Parse variadic functions
|
||||||
|
- [ ] Parse infix function definition
|
||||||
|
|
||||||
|
- [ ] Executer
|
||||||
|
- [x] Exec function calls
|
||||||
|
- [ ] Exec variable use
|
||||||
|
- [ ] Exec variable definition
|
||||||
|
- [ ] Exec function definition
|
||||||
|
- [ ] Exec symbolic variables
|
||||||
|
- [ ] Exec control flow statements
|
||||||
|
- [ ] Exec variadic functions
|
||||||
|
- [ ] Exec lambda functions
|
||||||
|
- [ ] Exec lists
|
||||||
|
- [ ] Exec arrays
|
||||||
|
- [ ] Exec vectors
|
||||||
|
|
||||||
|
- [ ] Interface
|
||||||
|
- [ ] Interactive interpreter
|
||||||
|
- [ ] Use GNU readline
|
||||||
|
- [ ] Multi-line input
|
||||||
|
- [ ] Syntax highlighting
|
||||||
|
- [ ] Autocompletion/suggestion
|
||||||
|
- [ ] Command line interface
|
||||||
|
- [ ] Pass in a file
|
||||||
|
- [ ] Save AST to a file
|
||||||
|
- [ ] Run from AST file
|
31
src/ast.c
31
src/ast.c
@@ -7,8 +7,9 @@
|
|||||||
extern AST* root;
|
extern AST* root;
|
||||||
|
|
||||||
static char* asttype_names[] = {
|
static char* asttype_names[] = {
|
||||||
[AST_TYPE_CALL] = "CALL",
|
[AST_TYPE_CALL] = "FUNC CALL",
|
||||||
[AST_TYPE_NUM] = "NUMBER",
|
[AST_TYPE_NUM] = "NUMBER",
|
||||||
|
[AST_TYPE_VREF] = "VAR REFERENCE"
|
||||||
};
|
};
|
||||||
|
|
||||||
AST* ast_init(ASTType type, void* data) {
|
AST* ast_init(ASTType type, void* data) {
|
||||||
@@ -29,6 +30,8 @@ void ast_destroy(AST* ast) {
|
|||||||
default:
|
default:
|
||||||
log_dbgf("Unknown ast type %d (max: %d)", ast->type, AST_TYPE_MAX);
|
log_dbgf("Unknown ast type %d (max: %d)", ast->type, AST_TYPE_MAX);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
free(ast);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ast_print(AST* ast) { ast_print_i(ast, 0); }
|
void ast_print(AST* ast) { ast_print_i(ast, 0); }
|
||||||
@@ -44,6 +47,7 @@ void ast_print_i(AST* ast, int i) {
|
|||||||
printf("%s %lf\n", INDENT_spacing->buf, *(ASTNumData*)ast->data);
|
printf("%s %lf\n", INDENT_spacing->buf, *(ASTNumData*)ast->data);
|
||||||
break;
|
break;
|
||||||
case AST_TYPE_CALL: ast_call_print(ast->data, i + 2); break;
|
case AST_TYPE_CALL: ast_call_print(ast->data, i + 2); break;
|
||||||
|
case AST_TYPE_VREF: ast_vref_print(ast->data, i + 2); break;
|
||||||
default: exit(1);
|
default: exit(1);
|
||||||
}
|
}
|
||||||
INDENT_FIELD_NONL_END;
|
INDENT_FIELD_NONL_END;
|
||||||
@@ -53,16 +57,12 @@ void ast_print_i(AST* ast, int i) {
|
|||||||
ASTNumData* ast_num_data_init(double val) {
|
ASTNumData* ast_num_data_init(double val) {
|
||||||
talloc(ASTNumData, num);
|
talloc(ASTNumData, num);
|
||||||
|
|
||||||
log_dbgf("val: %lf", val);
|
|
||||||
|
|
||||||
*num = val;
|
*num = val;
|
||||||
|
|
||||||
return num;
|
return num;
|
||||||
}
|
}
|
||||||
|
|
||||||
void ast_num_data_destroy(ASTNumData* num) {
|
void ast_num_data_destroy(ASTNumData* num) { free(num); }
|
||||||
if (!num) return free(num);
|
|
||||||
}
|
|
||||||
|
|
||||||
void ast_num_print(ASTNumData* data, int i) {
|
void ast_num_print(ASTNumData* data, int i) {
|
||||||
INDENT_BEGIN(i);
|
INDENT_BEGIN(i);
|
||||||
@@ -85,8 +85,10 @@ ASTCallData* ast_call_data_init(char* to, size_t argc, AST** argv) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void ast_call_data_destroy(ASTCallData* call) {
|
void ast_call_data_destroy(ASTCallData* call) {
|
||||||
if (!call) return free(call->to);
|
if (!call) return;
|
||||||
for (size_t i = 0; i < call->argc; i++) free(call->argv[i]);
|
free(call->to);
|
||||||
|
for (size_t i = 0; i < call->argc; i++) ast_destroy(call->argv[i]);
|
||||||
|
free(call->argv);
|
||||||
free(call);
|
free(call);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -100,3 +102,16 @@ void ast_call_print(ASTCallData* data, int i) {
|
|||||||
|
|
||||||
INDENT_END;
|
INDENT_END;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ASTVrefData* ast_vref_data_init(char* to) {}
|
||||||
|
|
||||||
|
void ast_vref_data_destroy(ASTVrefData* vref) {}
|
||||||
|
|
||||||
|
void ast_vref_print(ASTVrefData* data, int i) {
|
||||||
|
INDENT_BEGIN(i);
|
||||||
|
|
||||||
|
INDENT_TITLE("ASTVrefData", data);
|
||||||
|
INDENT_FIELD("to", "%s", data->to);
|
||||||
|
|
||||||
|
INDENT_END;
|
||||||
|
}
|
||||||
|
13
src/dstr.c
13
src/dstr.c
@@ -8,7 +8,7 @@
|
|||||||
Dstr* dstr_init(void) {
|
Dstr* dstr_init(void) {
|
||||||
Dstr* dstr = malloc(sizeof(Dstr));
|
Dstr* dstr = malloc(sizeof(Dstr));
|
||||||
|
|
||||||
dstr->bufsz = DSTR_INITSZ;
|
dstr->sz = DSTR_INITSZ;
|
||||||
dstr->buf = malloc(DSTR_INITSZ);
|
dstr->buf = malloc(DSTR_INITSZ);
|
||||||
*dstr->buf = '\0';
|
*dstr->buf = '\0';
|
||||||
dstr->ln = 0;
|
dstr->ln = 0;
|
||||||
@@ -21,15 +21,16 @@ void dstr_destroy(Dstr* dstr) {
|
|||||||
free(dstr);
|
free(dstr);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void dstr_destroypsv(Dstr* dstr) { free(dstr); }
|
||||||
|
|
||||||
// Check whether the buffer is overflowing and resize it if necessary.
|
// Check whether the buffer is overflowing and resize it if necessary.
|
||||||
void check_resz(Dstr* dstr, size_t ln) {
|
void check_resz(Dstr* dstr, size_t ln) {
|
||||||
while (dstr->ln + ln + 1 > dstr->bufsz) {
|
while (dstr->ln + ln + 1 > dstr->sz) {
|
||||||
// Double the buffer size when overflown.
|
// Double the buffer size when overflown.
|
||||||
dstr->bufsz *= 2;
|
dstr->sz *= 2;
|
||||||
dstr->buf = realloc(dstr->buf, dstr->bufsz);
|
dstr->buf = realloc(dstr->buf, dstr->sz);
|
||||||
log_dbgf(
|
log_dbgf(
|
||||||
"dstr @ %p doubled from %ld to %ld", dstr, dstr->bufsz / 2,
|
"dstr @ %p doubled from %ld to %ld", dstr, dstr->sz / 2, dstr->sz
|
||||||
dstr->bufsz
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
60
src/exec.c
60
src/exec.c
@@ -1,3 +1,4 @@
|
|||||||
|
#include <stddef.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
@@ -7,13 +8,13 @@
|
|||||||
|
|
||||||
extern AST* root;
|
extern AST* root;
|
||||||
|
|
||||||
ASTNumData exec_expr(AST* ast) {
|
ASTNumData exec_exp(AST* ast) {
|
||||||
// ast_print(ast);
|
|
||||||
log_dbg("Started execution.");
|
log_dbg("Started execution.");
|
||||||
switch (ast->type) {
|
switch (ast->type) {
|
||||||
case AST_TYPE_CALL: return exec_call(ast);
|
case AST_TYPE_CALL: return exec_call(ast);
|
||||||
case AST_TYPE_NUM: return *(ASTNumData*)ast->data;
|
case AST_TYPE_NUM: return *(ASTNumData*)ast->data;
|
||||||
default: printf("what\n");
|
default: printf("what\n");
|
||||||
|
exit(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -21,27 +22,48 @@ ASTNumData exec_call(AST* ast) {
|
|||||||
log_dbg("Started call execution.");
|
log_dbg("Started call execution.");
|
||||||
fflush(stdout);
|
fflush(stdout);
|
||||||
ASTCallData* calldata = (ASTCallData*)ast->data;
|
ASTCallData* calldata = (ASTCallData*)ast->data;
|
||||||
if (!strcmp(calldata->to, "sum") && calldata->argc == 2) {
|
if (calldata->argc >= 1) {
|
||||||
ASTNumData n1 = exec_expr(calldata->argv[0]);
|
if (!strcmp(calldata->to, "sum")) {
|
||||||
ASTNumData n2 = exec_expr(calldata->argv[1]);
|
double total = exec_exp(calldata->argv[0]);
|
||||||
|
|
||||||
return n1 + n2;
|
for (
|
||||||
} else if (!strcmp(calldata->to, "sub") && calldata->argc == 2) {
|
size_t i = 1;
|
||||||
ASTNumData n1 = exec_expr(calldata->argv[0]);
|
i < calldata->argc;
|
||||||
ASTNumData n2 = exec_expr(calldata->argv[1]);
|
total += exec_exp(calldata->argv[i++])
|
||||||
|
);
|
||||||
|
|
||||||
return n1 - n2;
|
return total;
|
||||||
} else if (!strcmp(calldata->to, "mul") && calldata->argc == 2) {
|
} else if (!strcmp(calldata->to, "sub")) {
|
||||||
ASTNumData n1 = exec_expr(calldata->argv[0]);
|
double total = exec_exp(calldata->argv[0]);
|
||||||
ASTNumData n2 = exec_expr(calldata->argv[1]);
|
|
||||||
|
|
||||||
return n1 * n2;
|
for (
|
||||||
} else if (!strcmp(calldata->to, "div") && calldata->argc == 2) {
|
size_t i = 1;
|
||||||
ASTNumData n1 = exec_expr(calldata->argv[0]);
|
i < calldata->argc;
|
||||||
ASTNumData n2 = exec_expr(calldata->argv[1]);
|
total -= exec_exp(calldata->argv[i++])
|
||||||
|
);
|
||||||
|
|
||||||
return n1 / n2;
|
return total;
|
||||||
}
|
} else if (!strcmp(calldata->to, "mul")) {
|
||||||
|
double total = exec_exp(calldata->argv[0]);
|
||||||
|
|
||||||
|
for (
|
||||||
|
size_t i = 1;
|
||||||
|
i < calldata->argc;
|
||||||
|
total *= exec_exp(calldata->argv[i++])
|
||||||
|
);
|
||||||
|
|
||||||
|
return total;
|
||||||
|
} else if (!strcmp(calldata->to, "div")) {
|
||||||
|
double total = exec_exp(calldata->argv[0]);
|
||||||
|
|
||||||
|
for (
|
||||||
|
size_t i = 1;
|
||||||
|
i < calldata->argc;
|
||||||
|
total /= exec_exp(calldata->argv[i++])
|
||||||
|
);
|
||||||
|
|
||||||
|
return total;
|
||||||
|
}}
|
||||||
return -1000;
|
return -1000;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -1,4 +1,5 @@
|
|||||||
%{
|
%{
|
||||||
|
#include <string.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include "../../src/include/ast.h"
|
#include "../../src/include/ast.h"
|
||||||
#include "../../src/include/lexer.h"
|
#include "../../src/include/lexer.h"
|
||||||
@@ -17,6 +18,7 @@
|
|||||||
double fval;
|
double fval;
|
||||||
char* strval;
|
char* strval;
|
||||||
AST* ast;
|
AST* ast;
|
||||||
|
ArgArr* argarr;
|
||||||
}
|
}
|
||||||
|
|
||||||
%define parse.error verbose
|
%define parse.error verbose
|
||||||
@@ -25,7 +27,7 @@
|
|||||||
%token RGROUP
|
%token RGROUP
|
||||||
%token SEP
|
%token SEP
|
||||||
|
|
||||||
%token<strval> CALL
|
%token<strval> WORD
|
||||||
%token<fval> NUM
|
%token<fval> NUM
|
||||||
|
|
||||||
%token SUB
|
%token SUB
|
||||||
@@ -35,12 +37,13 @@
|
|||||||
|
|
||||||
%token NL
|
%token NL
|
||||||
|
|
||||||
%left DIV PLUS MULT SUB
|
%left PLUS SUB
|
||||||
|
%left MULT DIV
|
||||||
%precedence NEG
|
%precedence NEG
|
||||||
|
|
||||||
%type<fval> num;
|
|
||||||
%type<ast> exp;
|
%type<ast> exp;
|
||||||
|
%type<argarr> arg;
|
||||||
|
%type<argarr> argstart;
|
||||||
|
|
||||||
%%
|
%%
|
||||||
|
|
||||||
@@ -49,47 +52,80 @@ input:
|
|||||||
| exp { root = $1; }
|
| exp { root = $1; }
|
||||||
;
|
;
|
||||||
|
|
||||||
num:
|
|
||||||
NUM { $$ = $1; }
|
argstart:
|
||||||
| SUB NUM { $$ = -$2; } %prec NEG
|
exp {
|
||||||
|
ArgArr* argarr = argarr_init();
|
||||||
|
argarr_add(argarr, $1);
|
||||||
|
$$ = argarr;
|
||||||
|
}
|
||||||
|
;
|
||||||
|
|
||||||
|
arg:
|
||||||
|
argstart { $$ = $1; }
|
||||||
|
| arg SEP exp {
|
||||||
|
argarr_add($1, $3);
|
||||||
|
$$ = $1;
|
||||||
|
}
|
||||||
;
|
;
|
||||||
|
|
||||||
exp:
|
exp:
|
||||||
num { $$ = ast_init(AST_TYPE_NUM, ast_num_data_init($1)); }
|
NUM { $$ = ast_init(AST_TYPE_NUM, ast_num_data_init($1)); }
|
||||||
|
|
||||||
// name(thing, thing)
|
| SUB exp {
|
||||||
| CALL LGROUP exp SEP exp RGROUP {
|
|
||||||
AST** argv = calloc(2, sizeof(AST*));
|
AST** argv = calloc(2, sizeof(AST*));
|
||||||
argv[0] = $3;
|
argv[0] = ast_init(AST_TYPE_NUM, ast_num_data_init(-1));
|
||||||
argv[1] = $5;
|
argv[1] = $2;
|
||||||
$$ = ast_init(AST_TYPE_CALL, ast_call_data_init($1, 2, argv));
|
char* to = malloc(4);
|
||||||
|
strcpy(to, "mul");
|
||||||
|
$$ = ast_init(AST_TYPE_CALL, ast_call_data_init(to, 2, argv));
|
||||||
|
}
|
||||||
|
|
||||||
|
| LGROUP exp RGROUP { $$ = $2; }
|
||||||
|
|
||||||
|
// Variable reference.
|
||||||
|
//| WORD
|
||||||
|
|
||||||
|
| WORD LGROUP arg RGROUP {
|
||||||
|
size_t argc = $3->ln;
|
||||||
|
AST** argv = $3->buf;
|
||||||
|
argarr_destroypsv($3);
|
||||||
|
$$ = ast_init(AST_TYPE_CALL, ast_call_data_init($1, argc, argv));
|
||||||
}
|
}
|
||||||
|
|
||||||
| exp PLUS exp {
|
| exp PLUS exp {
|
||||||
AST** argv = calloc(2, sizeof(AST*));
|
AST** argv = calloc(2, sizeof(AST*));
|
||||||
argv[0] = $1;
|
argv[0] = $1;
|
||||||
argv[1] = $3;
|
argv[1] = $3;
|
||||||
$$ = ast_init(AST_TYPE_CALL, ast_call_data_init("sum", 2, argv));
|
char* to = malloc(4);
|
||||||
|
strcpy(to, "sum");
|
||||||
|
$$ = ast_init(AST_TYPE_CALL, ast_call_data_init(to, 2, argv));
|
||||||
}
|
}
|
||||||
|
|
||||||
| exp SUB exp {
|
| exp SUB exp {
|
||||||
AST** argv = calloc(2, sizeof(AST*));
|
AST** argv = calloc(2, sizeof(AST*));
|
||||||
argv[0] = $1;
|
argv[0] = $1;
|
||||||
argv[1] = $3;
|
argv[1] = $3;
|
||||||
$$ = ast_init(AST_TYPE_CALL, ast_call_data_init("sub", 2, argv));
|
char* to = malloc(4);
|
||||||
|
strcpy(to, "sub");
|
||||||
|
$$ = ast_init(AST_TYPE_CALL, ast_call_data_init(to, 2, argv));
|
||||||
}
|
}
|
||||||
|
|
||||||
| exp MULT exp {
|
| exp MULT exp {
|
||||||
AST** argv = calloc(2, sizeof(AST*));
|
AST** argv = calloc(2, sizeof(AST*));
|
||||||
argv[0] = $1;
|
argv[0] = $1;
|
||||||
argv[1] = $3;
|
argv[1] = $3;
|
||||||
$$ = ast_init(AST_TYPE_CALL, ast_call_data_init("mul", 2, argv));
|
char* to = malloc(4);
|
||||||
|
strcpy(to, "mul");
|
||||||
|
$$ = ast_init(AST_TYPE_CALL, ast_call_data_init(to, 2, argv));
|
||||||
}
|
}
|
||||||
|
|
||||||
| exp DIV exp {
|
| exp DIV exp {
|
||||||
AST** argv = calloc(2, sizeof(AST*));
|
AST** argv = calloc(2, sizeof(AST*));
|
||||||
argv[0] = $1;
|
argv[0] = $1;
|
||||||
argv[1] = $3;
|
argv[1] = $3;
|
||||||
$$ = ast_init(AST_TYPE_CALL, ast_call_data_init("div", 2, argv));
|
char* to = malloc(4);
|
||||||
|
strcpy(to, "div");
|
||||||
|
$$ = ast_init(AST_TYPE_CALL, ast_call_data_init(to, 2, argv));
|
||||||
}
|
}
|
||||||
%%
|
%%
|
||||||
|
@@ -4,8 +4,9 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
typedef enum {
|
typedef enum {
|
||||||
AST_TYPE_NUM,
|
AST_TYPE_NUM, // A number.
|
||||||
AST_TYPE_CALL,
|
AST_TYPE_CALL, // A function call.
|
||||||
|
AST_TYPE_VREF, // A variable reference.
|
||||||
AST_TYPE_MAX = AST_TYPE_CALL
|
AST_TYPE_MAX = AST_TYPE_CALL
|
||||||
} ASTType;
|
} ASTType;
|
||||||
|
|
||||||
@@ -26,13 +27,21 @@ void ast_num_data_destroy(ASTNumData* num);
|
|||||||
void ast_num_print(ASTNumData*, int i);
|
void ast_num_print(ASTNumData*, int i);
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
char* to; // What the call's to.
|
char* to; // What the call's to.
|
||||||
size_t argc; // Argument count.
|
size_t argc; // Argument count.
|
||||||
AST** argv; // Argument vector.
|
AST** argv; // Argument vector.
|
||||||
} ASTCallData;
|
} ASTCallData;
|
||||||
|
|
||||||
ASTCallData* ast_call_data_init(char* to, size_t argc, AST** argv);
|
ASTCallData* ast_call_data_init(char* to, size_t argc, AST** argv);
|
||||||
void ast_call_data_destroy(ASTCallData* call);
|
void ast_call_data_destroy(ASTCallData* call);
|
||||||
void ast_call_print(ASTCallData*, int i);
|
void ast_call_print(ASTCallData*, int i);
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
char* to; // What the reference's to.
|
||||||
|
} ASTVrefData;
|
||||||
|
|
||||||
|
ASTVrefData* ast_vref_data_init(char* to);
|
||||||
|
void ast_vref_data_destroy(ASTVrefData* call);
|
||||||
|
void ast_vref_print(ASTVrefData*, int i);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@@ -3,16 +3,18 @@
|
|||||||
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
#define DSTR_INITSZ 128
|
#define DSTR_INITSZ 2
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
char* buf; // The buffer containing the string.
|
char* buf; // The buffer containing the string.
|
||||||
size_t bufsz; // The size of the buffer.
|
size_t sz; // The size of the buffer.
|
||||||
size_t ln; // The number of characters in the buffer.
|
size_t ln; // The number of characters in the buffer.
|
||||||
} Dstr;
|
} Dstr;
|
||||||
|
|
||||||
Dstr* dstr_init(void);
|
Dstr* dstr_init(void);
|
||||||
void dstr_destroy(Dstr* dstr);
|
void dstr_destroy(Dstr* dstr);
|
||||||
|
// Destroy Dstr structure but preserve ->buf.
|
||||||
|
void dstr_destroypsv(Dstr* dstr);
|
||||||
|
|
||||||
// Append ln characters of src to dest.
|
// Append ln characters of src to dest.
|
||||||
void dstr_append(Dstr* dest, char* src, size_t ln);
|
void dstr_append(Dstr* dest, char* src, size_t ln);
|
||||||
|
@@ -3,7 +3,7 @@
|
|||||||
|
|
||||||
#include "ast.h"
|
#include "ast.h"
|
||||||
|
|
||||||
ASTNumData exec_expr(AST* ast);
|
ASTNumData exec_exp(AST* ast);
|
||||||
ASTNumData exec_call(AST* ast);
|
ASTNumData exec_call(AST* ast);
|
||||||
void exec_print(double n);
|
void exec_print(double n);
|
||||||
|
|
||||||
|
@@ -2,11 +2,29 @@
|
|||||||
#define LEXER_H
|
#define LEXER_H
|
||||||
|
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
|
#include <stddef.h>
|
||||||
|
|
||||||
|
#include "ast.h"
|
||||||
|
|
||||||
|
#define ARLN 8
|
||||||
|
|
||||||
|
extern char* inp;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
size_t sz;
|
||||||
|
size_t ln;
|
||||||
|
AST** buf;
|
||||||
|
} ArgArr;
|
||||||
|
|
||||||
|
ArgArr* argarr_init();
|
||||||
|
void argarr_destroy(ArgArr* argarr);
|
||||||
|
// Destroy ArgArr structure but preserve -> buf.
|
||||||
|
void argarr_destroypsv(ArgArr* argarr);
|
||||||
|
void argarr_add(ArgArr* argarr, AST* arg);
|
||||||
|
|
||||||
#include "../../build/grammars/grammar.tab.h"
|
#include "../../build/grammars/grammar.tab.h"
|
||||||
|
|
||||||
extern YYSTYPE yylval;
|
extern YYSTYPE yylval;
|
||||||
extern char* inp;
|
|
||||||
|
|
||||||
// Accumulate an integer.
|
// Accumulate an integer.
|
||||||
int acc_int(int c);
|
int acc_int(int c);
|
||||||
|
40
src/lexer.c
40
src/lexer.c
@@ -1,3 +1,4 @@
|
|||||||
|
#include <complex.h>
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
#include <limits.h>
|
#include <limits.h>
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
@@ -5,6 +6,37 @@
|
|||||||
|
|
||||||
#include "include/dstr.h"
|
#include "include/dstr.h"
|
||||||
#include "include/lexer.h"
|
#include "include/lexer.h"
|
||||||
|
#include "include/util.h"
|
||||||
|
|
||||||
|
ArgArr* argarr_init() {
|
||||||
|
ArgArr* argarr = malloc(sizeof(ArgArr));
|
||||||
|
|
||||||
|
argarr->sz = ARLN * sizeof(AST*);
|
||||||
|
argarr->ln = 0;
|
||||||
|
argarr->buf = malloc(argarr->sz);
|
||||||
|
|
||||||
|
return argarr;
|
||||||
|
}
|
||||||
|
|
||||||
|
void argarr_destroy(ArgArr* argarr) {
|
||||||
|
free(argarr->buf);
|
||||||
|
free(argarr);
|
||||||
|
}
|
||||||
|
|
||||||
|
void argarr_destroypsv(ArgArr* argarr) { free(argarr); }
|
||||||
|
|
||||||
|
void argarr_add(ArgArr* argarr, AST* arg) {
|
||||||
|
if ((argarr->ln + 1) * argarr->sz > argarr->sz) {
|
||||||
|
argarr->sz *= 2;
|
||||||
|
argarr->buf = realloc(argarr->buf, argarr->sz);
|
||||||
|
log_dbgf(
|
||||||
|
"ArgArr @ %p doubled from %ld to %ld", argarr, argarr->sz / 2,
|
||||||
|
argarr->sz
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
argarr->buf[argarr->ln++] = arg;
|
||||||
|
}
|
||||||
|
|
||||||
int acc_int(int c) {
|
int acc_int(int c) {
|
||||||
int value = c - '0';
|
int value = c - '0';
|
||||||
@@ -46,14 +78,16 @@ char* acc_word(int c) {
|
|||||||
} while (isalpha(*inp));
|
} while (isalpha(*inp));
|
||||||
dstr_appendch(val, *(inp - 1));
|
dstr_appendch(val, *(inp - 1));
|
||||||
|
|
||||||
return val->buf;
|
char* ret = val->buf;
|
||||||
|
dstr_destroypsv(val);
|
||||||
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
int yylex() {
|
int yylex() {
|
||||||
if (*inp == '\0') return YYEOF;
|
if (*inp == '\0') return YYEOF;
|
||||||
|
|
||||||
// Skip all whitespace.
|
// Skip all whitespace.
|
||||||
while (*inp == ' ' || *inp == '\t') { inp++; }
|
while (*inp == ' ' || *inp == '\t') inp++;
|
||||||
|
|
||||||
// Assign & consume current character.
|
// Assign & consume current character.
|
||||||
int c = *inp++;
|
int c = *inp++;
|
||||||
@@ -66,7 +100,7 @@ int yylex() {
|
|||||||
|
|
||||||
if (isalpha(c)) {
|
if (isalpha(c)) {
|
||||||
yylval.strval = acc_word(c);
|
yylval.strval = acc_word(c);
|
||||||
return CALL;
|
return WORD;
|
||||||
}
|
}
|
||||||
|
|
||||||
switch (c) {
|
switch (c) {
|
||||||
|
13
src/main.c
13
src/main.c
@@ -1,4 +1,5 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
#include "include/ast.h"
|
#include "include/ast.h"
|
||||||
#include "include/dstr.h"
|
#include "include/dstr.h"
|
||||||
@@ -18,6 +19,13 @@ extern int yyparse();
|
|||||||
|
|
||||||
int main(int argc, char** argv) {
|
int main(int argc, char** argv) {
|
||||||
|
|
||||||
|
if (argc - 1 && strlen(argv[1]) > 0 && (inp = argv[1]) && !yyparse()) {
|
||||||
|
log_dbg("Parsed successfully!\n");
|
||||||
|
exec_print(exec_exp(root));
|
||||||
|
ast_destroy(root);
|
||||||
|
exit(0);
|
||||||
|
}
|
||||||
|
|
||||||
while (1) {
|
while (1) {
|
||||||
Dstr* ln = dstr_init();
|
Dstr* ln = dstr_init();
|
||||||
char c;
|
char c;
|
||||||
@@ -45,13 +53,16 @@ int main(int argc, char** argv) {
|
|||||||
log_dbg("Parsed successfully!\n");
|
log_dbg("Parsed successfully!\n");
|
||||||
} else printf("Parse error.\n");
|
} else printf("Parse error.\n");
|
||||||
|
|
||||||
exec_print(exec_expr(root));
|
exec_print(exec_exp(root));
|
||||||
#ifdef DBG
|
#ifdef DBG
|
||||||
ast_print(root);
|
ast_print(root);
|
||||||
#endif
|
#endif
|
||||||
|
ast_destroy(root);
|
||||||
}
|
}
|
||||||
|
|
||||||
dstr_destroy(ln);
|
dstr_destroy(ln);
|
||||||
}
|
}
|
||||||
lnskip:;
|
lnskip:;
|
||||||
|
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
84
test/validation/test.bats
Normal file
84
test/validation/test.bats
Normal file
@@ -0,0 +1,84 @@
|
|||||||
|
#!/usr/bin/env bats
|
||||||
|
|
||||||
|
bin() { ./scl.out $1 | tail -n1; }
|
||||||
|
|
||||||
|
@test "simple addition" {
|
||||||
|
run bin "1+1"
|
||||||
|
[ "$output" = "= 2.000000" ]
|
||||||
|
|
||||||
|
run bin "-1+1"
|
||||||
|
[ "$output" = "= 0.000000" ]
|
||||||
|
|
||||||
|
run bin "1+-1"
|
||||||
|
[ "$output" = "= 0.000000" ]
|
||||||
|
|
||||||
|
run bin "-1+-1"
|
||||||
|
[ "$output" = "= -2.000000" ]
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "simple subtraction" {
|
||||||
|
run bin "1-1"
|
||||||
|
[ "$output" = "= 0.000000" ]
|
||||||
|
|
||||||
|
run bin "-1-1"
|
||||||
|
[ "$output" = "= -2.000000" ]
|
||||||
|
|
||||||
|
run bin "1--1"
|
||||||
|
[ "$output" = "= 2.000000" ]
|
||||||
|
|
||||||
|
run bin "-1--1"
|
||||||
|
[ "$output" = "= 0.000000" ]
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "simple multiplication" {
|
||||||
|
run bin "1*2"
|
||||||
|
[ "$output" = "= 2.000000" ]
|
||||||
|
|
||||||
|
run bin "-1*2"
|
||||||
|
[ "$output" = "= -2.000000" ]
|
||||||
|
|
||||||
|
run bin "1*-1"
|
||||||
|
[ "$output" = "= -1.000000" ]
|
||||||
|
|
||||||
|
run bin "-1*-1"
|
||||||
|
[ "$output" = "= 1.000000" ]
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "simple division" {
|
||||||
|
run bin "1/2"
|
||||||
|
[ "$output" = "= 0.500000" ]
|
||||||
|
|
||||||
|
run bin "-1/2"
|
||||||
|
[ "$output" = "= -0.500000" ]
|
||||||
|
|
||||||
|
run bin "1/-1"
|
||||||
|
[ "$output" = "= -1.000000" ]
|
||||||
|
|
||||||
|
run bin "-1/-1"
|
||||||
|
[ "$output" = "= 1.000000" ]
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "order of operations" {
|
||||||
|
run bin "1+2*3"
|
||||||
|
[ "$output" = "= 7.000000" ]
|
||||||
|
|
||||||
|
run bin "2*3+1"
|
||||||
|
[ "$output" = "= 7.000000" ]
|
||||||
|
|
||||||
|
run bin "6/2-1"
|
||||||
|
[ "$output" = "= 2.000000" ]
|
||||||
|
|
||||||
|
run bin "1-6/2"
|
||||||
|
[ "$output" = "= -2.000000" ]
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "order of operations with parenthesis" {
|
||||||
|
run bin "(1+2)*3"
|
||||||
|
[ "$output" = "= 9.000000" ]
|
||||||
|
|
||||||
|
run bin "-(1+2*3)"
|
||||||
|
[ "$output" = "= -7.000000" ]
|
||||||
|
|
||||||
|
run bin "-(-(1+2)*3)"
|
||||||
|
[ "$output" = "= 9.000000" ]
|
||||||
|
}
|
Reference in New Issue
Block a user