Compare commits

88 Commits

Author SHA1 Message Date
694eb43eab Bump version. 2025-01-18 10:47:10 -05:00
694d40124f Bump version. 2025-01-18 10:46:21 -05:00
39778ce08d Bump version. 2025-01-18 10:44:13 -05:00
cfd44621d5 Whoops a bit overzealous with search and replace. 2025-01-18 10:43:18 -05:00
4be71317b0 Fixed memory leaks. 2025-01-18 10:41:54 -05:00
49642553e1 Cleaned up Makefile. 2025-01-18 10:41:34 -05:00
1e11b5921d It all works! (except for mem leaks) 2025-01-18 09:42:28 -05:00
7343c3b9d9 Fixed some function parsing. 2025-01-17 11:49:51 -05:00
9e8410d4cf Parens. 2025-01-16 11:59:34 -05:00
bc0c4f33ad Cleaned up. 2025-01-14 15:11:20 -05:00
1098fa252f Added parenthesis.
Also updated some grammar rules for negatives to be more general.
2025-01-14 13:59:26 -05:00
0b1905429c Updated tests to include order of operations. 2025-01-12 20:47:24 -05:00
e7d3ea3697 Fixed testing recipe. 2025-01-12 20:47:06 -05:00
577bde6e57 Fixed list indentation. 2025-01-12 20:36:24 -05:00
60b9ed9eb2 Updated README and added STATUS. 2025-01-12 20:34:43 -05:00
d13bf883b5 Fixed some functions. 2025-01-11 11:05:26 -05:00
681e005a68 Generalized arithmetic functions
to arbitrary arguments.
2025-01-11 10:53:49 -05:00
2ce89fb39a Added arbitrary length functions. 2025-01-11 10:35:42 -05:00
907bc26264 Updated README.md. 2025-01-11 09:24:20 -05:00
e243e862ae Updated README.md. 2025-01-11 09:23:14 -05:00
5e930b9847 Updated README.
Why can't the link be in <pre>??
2025-01-09 11:43:49 -05:00
ed3ec885c0 Updated README.
OK so apparently I don't know how markdown links work...
2025-01-09 11:43:04 -05:00
835bcfe121 Updated README. 2025-01-09 11:42:25 -05:00
9432496875 Updated README. 2025-01-09 11:42:06 -05:00
0731e40e6a Fixed precedence. 2025-01-04 14:01:05 -05:00
35322de3ac Added validation tests to Makefile. 2025-01-04 10:48:01 -05:00
c3d8d6f8e5 Validation tests. 2025-01-04 10:45:34 -05:00
1f2bca5028 Not sure. 2025-01-04 10:45:11 -05:00
9a9e5cd3e0 Fixed parsing of negative numbers. 2025-01-04 09:53:39 -05:00
4d828500af Updated README. 2024-12-28 19:03:18 -05:00
deac5ca5b8 Updated README. 2024-12-28 19:02:36 -05:00
e05ebeef2a Added the ability to call infix function backend. 2024-12-28 18:59:04 -05:00
bdca40bae4 Made the infix ops call the normal functions. 2024-12-28 18:42:37 -05:00
b4cd46a1e7 Added division. 2024-12-28 18:39:33 -05:00
a57acc1176 Fixed stupid stupid GNU Make issue. 2024-12-28 12:18:36 -05:00
ffcf2fb013 Added tests for dstr. 2024-12-28 11:55:35 -05:00
c7a9c8215c Reorganized functions, consolidated. 2024-12-28 11:54:58 -05:00
ca4cf2cd68 Finished Unity test support.
Not really, it fails to build tests sometimes and I have no idea why.
2024-12-28 11:52:59 -05:00
e19fc8820a Added Unity in test. 2024-12-28 09:30:35 -05:00
ee3f2919c6 Update README.md. Updated main.c. 2024-12-21 11:10:01 -05:00
653736622f Add multiplication.
Supports negative numbers too.
2024-12-21 10:45:43 -05:00
e3afe52ab7 Added negative numbers. 2024-12-21 10:33:36 -05:00
905acacd07 Fixed float addition.
One can now add floats, will also print the AST.
2024-12-21 10:12:30 -05:00
8763fa35dd Updated README.md 2024-12-14 20:35:36 -05:00
feae5d560a Trimmed grammars. 2024-12-07 11:02:22 -05:00
ec268f6047 Nothing works. 2024-12-07 11:01:00 -05:00
6fff5e5cc8 Without token.c. 2024-12-07 10:33:30 -05:00
7b19e553f2 With token.c. 2024-12-07 10:33:16 -05:00
64ef797727 Things. 2024-12-07 09:16:17 -05:00
85e17ede84 Addition with floats is now entirely possible. 2024-11-30 11:15:18 -05:00
8e5b39a6e4 Can now do integer addition (with floats! :D). 2024-11-30 10:44:21 -05:00
4514d94be9 Basic addition with integers is now available. 2024-11-30 10:24:31 -05:00
4080d1a80a Cleaned up. 2024-11-23 10:21:34 -05:00
a36ae22d52 Added exec, grammar files. 2024-11-23 09:30:35 -05:00
ad8ac61b98 Parse. 2024-11-16 10:42:50 -05:00
e5bb4dfd96 Now compiles without warning. 2024-11-16 10:15:30 -05:00
363188d7d6 The. 2024-11-16 10:00:40 -05:00
139d6fcb22 Things. 2024-11-09 11:09:57 -05:00
92d9da14c2 Grammars are broken. 2024-11-09 10:27:03 -05:00
b43fd46260 Changes were made. 2024-11-09 04:37:56 -05:00
a6dc46149c README.md 2024-11-07 19:55:54 -05:00
120038ea8f Global lexer. 2024-11-07 19:41:14 -05:00
8cf09e43c9 Worked on yylex(). 2024-11-02 11:02:18 -04:00
ecc12f6f3b Begin bison-based parsing. 2024-11-02 10:31:55 -04:00
a1f210fee1 Something. 2024-10-31 16:44:17 -04:00
2662f54c51 Fixed more print formatting. 2024-10-31 16:05:04 -04:00
950c25bace Fixed print formatting. 2024-10-31 12:52:39 -04:00
1c0dd7aa0b Added stacks to parser. 2024-10-26 11:02:42 -04:00
5b345e6bf5 Added stack. 2024-10-26 10:56:11 -04:00
891d8bf7ef Finished initial AST structures.
Call and ints (nums).
2024-10-26 10:07:33 -04:00
68fc644ea6 Added more AST stuff. 2024-10-25 11:23:03 -04:00
ca9d2aabe4 Fixed more printing, added basic ast. 2024-10-25 11:20:07 -04:00
7a04ccfd9f Fixed some printing errors, introduced many more. 2024-10-19 10:59:05 -04:00
4df9808859 Fixed blinking functions.
Heh.
2024-10-19 09:15:03 -04:00
933418895e Something. 2024-10-19 09:09:37 -04:00
3c56290448 Numbers work. Calls do not work. 2024-10-16 08:13:32 -04:00
63f5064ba9 Fixed some things, broke others. 2024-10-13 23:46:03 -04:00
8e8b6233d6 Added dynamic strings. They do not yet work. 2024-10-10 16:09:25 -04:00
89e8674ced Update README.md. 2024-10-07 11:54:54 -04:00
77f40cf3c5 Changed things. 2024-10-07 11:48:53 -04:00
2008bab1f7 Fixed tests. 2024-10-05 09:35:33 -04:00
d244cfbfe1 Tests are slightly not. 2024-10-05 09:24:12 -04:00
6f283c1d12 Fixed some things. 2024-10-02 21:06:54 -04:00
aae8402403 Added .clang-format. 2024-10-02 21:04:54 -04:00
53c303095d ...whoops. 2024-10-02 20:05:46 -04:00
ce25c5fe9f Beginnings of the lexer. 2024-10-02 17:57:04 -04:00
d95c134a54 Test and stuff.
Need to fix testing functions so that every assert is run in each
function of the registry.
2024-09-30 20:55:46 -04:00
32386ccf58 Here's another commit.
Got the Makefile working with tests properly.
2024-09-30 09:00:37 -04:00
28 changed files with 1221 additions and 66 deletions

13
.clang-format Normal file
View File

@@ -0,0 +1,13 @@
---
AlignConsecutiveShortCaseStatements:
Enabled: true
AcrossEmptyLines: true
AcrossComments: true
IndentCaseLabels: true
AllowShortBlocksOnASingleLine: Always
AllowShortCaseLabelsOnASingleLine: true
AllowShortIfStatementsOnASingleLine: AllIfsAndElse
AllowShortLoopsOnASingleLine: true
IndentWidth: 4
PointerAlignment: Left
AlignAfterOpenBracket: BlockIndent

2
.clangd Normal file
View File

@@ -0,0 +1,2 @@
CompileFlags:
Add: [-xc]

6
.gitignore vendored
View File

@@ -1,6 +1,8 @@
*.o
*.so
tags
*.out
obj/
compile_commands.json
.cache
build/*
compile_commands.json
vgcore.*

3
.gitmodules vendored Normal file
View File

@@ -0,0 +1,3 @@
[submodule "test/Unity"]
path = test/Unity
url = https://github.com/ThrowTheSwitch/Unity

104
Makefile
View File

@@ -1,29 +1,105 @@
NAME = scl
CC = clang
CFLAGS = -Wall --std=c11
LDFLAGS =
TARGET = $(NAME).out
SRC_DIR = src
OBJ_DIR = obj
TARGET = $(NAME).out
INC_DIR = $(SRC_DIR)/include
BUILD_DIR = build
OBJ_DIR = $(BUILD_DIR)/obj
GRAM_DIR = $(BUILD_DIR)/grammars
TEST_DIR = test
TEST_BUILD_DIR = $(BUILD_DIR)/test
TEST_OBJ_DIR = $(TEST_BUILD_DIR)/obj
CC = clang
LINK = clang
CFLAGS = -Wall -DDBG -ggdb
LDFLAGS = -lm
BATS = bats
BISON = bison
PRINT = echo -e
SRC_FILES = $(wildcard $(SRC_DIR)/*.c)
OBJ_FILES = $(patsubst $(SRC_DIR)/%.c, $(OBJ_DIR)/%.o, $(SRC_FILES))
OBJ_FILES_NOMAIN = $(filter-out $(OBJ_DIR)/main.o, $(OBJ_FILES)) # Object files without main.c.
GRAM_FILES = $(GRAM_DIR)/grammar.tab.c $(GRAM_DIR)/grammar.tab.h
UNITY_DIR = $(TEST_DIR)/Unity
UNITY_C = $(UNITY_DIR)/src/unity.c
UNITY_H = $(UNITY_DIR)/src/unity.h
UNITY_OBJ = $(TEST_BUILD_DIR)/unity.o
TEST_SRC_FILES = $(wildcard $(TEST_DIR)/*.c)
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_VAL_DIR = $(TEST_DIR)/validation
RESETCOLOR = \033[0m
WHITE = $(RESETCOLOR)\033[37m
WHITE_BOLD = $(RESETCOLOR)\033[37;1m
all: $(TARGET)
$(TARGET): $(OBJ_FILES)
@ echo -e "\x1b[32;1mLinking \x1b[0m\x1b[32m$(TARGET)\x1b[32;1m...\x1b[0m\x1b[37m $(CC) -o $(TARGET) $(OBJ_FILES) $(LDFLAGS)\x1b[0m"
@ $(CC) -o $(TARGET) $(OBJ_FILES) $(LDFLAGS)
release: clean
release: CFLAGS = -Wall -O2
release: $(TARGET)
$(OBJ_DIR)/%.o: $(SRC_DIR)/%.c $(SRC_DIR)/include/%.h
# Run the target.
run: $(TARGET)
./$(TARGET)
# Generate grammars with bison.
$(GRAM_FILES): $(SRC_DIR)/grammar.y
@ mkdir -p $(GRAM_DIR)
@ $(PRINT) "$(WHITE_BOLD)Generating grammars...$(RESETCOLOR)"
$(BISON) $< -o$(GRAM_DIR)/grammar.tab.c -H$(GRAM_DIR)/grammar.tab.h
# Compile grammars.
$(OBJ_DIR)/grammar.o: $(GRAM_DIR)/grammar.tab.c $(GRAM_DIR)/grammar.tab.h $(OBJ_DIR)/lexer.o
@ $(PRINT) "$(WHITE_BOLD)Compiling grammars...$(RESETCOLOR)"
$(CC) $(CFLAGS) -c $< -o $@
# Lexer depends on grammars.
$(OBJ_DIR)/lexer.o: $(SRC_DIR)/lexer.c $(GRAM_FILES)
@ mkdir -p $(OBJ_DIR)
@ echo -e "\x1b[32;1mCompiling \x1b[0m\x1b[32m$<\x1b[32;1m... \x1b[0m\x1b[37m$(CC) $(CFLAGS) -c $< -o $@\x1b[0m"
@ $(CC) $(CFLAGS) -c $< -o $@
@ $(PRINT) "$(WHITE_BOLD)Compiling source object $(WHITE)$@$(WHITE_BOLD)... $(RESETCOLOR)"
$(CC) $(CFLAGS) -c $< -o $@
# Compile project source objects.
$(OBJ_DIR)/%.o: $(SRC_DIR)/%.c $(INC_DIR)/%.h
@ mkdir -p $(OBJ_DIR)
@ $(PRINT) "$(WHITE_BOLD)Compiling source object $(WHITE)$@$(WHITE_BOLD)... $(RESETCOLOR)"
$(CC) $(CFLAGS) -c $< -o $@
# Link to final binary.
$(TARGET): $(OBJ_DIR)/grammar.o $(OBJ_FILES)
@ $(PRINT) "$(WHITE_BOLD)Linking $(WHITE)$@$(WHITE_BOLD)...$(RESETCOLOR)"
$(LINK) -o $(TARGET) $(OBJ_FILES) $(OBJ_DIR)/grammar.o $(LDFLAGS)
# Compile Unity object.
$(UNITY_OBJ): $(UNITY_C) $(UNITY_H)
@ $(PRINT) "$(WHITE_BOLD)Compiling Unity...$(RESETCOLOR)"
$(CC) $(CFLAGS) -D UNITY_OUTPUT_COLOR -c $< -o $@
# Compile test object.
$(TEST_OBJ_DIR)/test_%.o: $(TEST_DIR)/test_%.c
@ $(PRINT) "$(WHITE_BOLD)Compiling test object $(WHITE)$@$(WHITE_BOLD)...$(RESETCOLOR)"
$(CC) $(CFLAGS) -c $< -o $@
# Link final test binary.
$(TEST_BUILD_DIR)/test_%.out: $(TEST_OBJ_DIR)/test_%.o $(OBJ_DIR)/%.o $(UNITY_OBJ)
@ $(PRINT) "$(WHITE_BOLD)Linking test binary $(WHITE)$@$(WHITE_BOLD)...$(RESETCOLOR)"
$(LINK) -o $@ $? $(LDFLAGS)
# Run the test files.
test: $(TARGET) $(TEST_BIN_FILES)
@ $(PRINT) "$(WHITE_BOLD)Running unit tests...$(RESETCOLOR)"
for test in $(TEST_BIN_FILES); do ./$${test}; done
@ $(PRINT) "$(WHITE_BOLD)Running validation tests...$(RESETCOLOR)"
$(BATS) $(TEST_VAL_DIR)
clean:
@ echo -e "\x1b[32;1mCleaning up...\x1b[0m"
@ rm -rf $(OBJ_DIR) $(TARGET)
@ $(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)
.PHONY: all clean
.PHONY: all clean test nocolor release run
.PRECIOUS: $(TEST_OBJ_FILES)

View File

@@ -1 +1,62 @@
# SCL: Simple Calculator Language
# SCL: Simple CAS Language
Version v1.0-alpha
## Usage
```bash
git clone https://git.signorovitch.org/jacob/scl && cd scl
make release # Build.
./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
See [STATUS.md](STATUS.md). Currently, one is able to use `scl` as a basic,
interactive, four-function calculator.
## Syntax (Planned)
As one would expect, you can evaluate simple infix expressions:
```scl
> 1 + 1
= 2
```
You can also define your own functions:
```scl
> f(x) = 2x
> f(2)
= 4
```
Symbolic algebra is done in the following manner:
```scl
> f(x) = x^4
> diff(f, x:sym, 2)
= 12x^2
```
SCL will dynamically decide on types, but you can state them explicitly as
well:
```scl
> f(x:int) = 2x
> f(2.2)
! f(x:int): x must be of type int.
```
Variables can be defined, with several attributes:
```scl
> a = 1 // Interpret type automatically.
> b:int = 1 // Must be int.
> c:const:int = 1 // Constant: value can never change.
> x:sym // Treated symbolicaly.
```

55
STATUS.md Normal file
View 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

1
examples/test.scl Normal file
View File

@@ -0,0 +1 @@
1 + 1

117
src/ast.c Normal file
View File

@@ -0,0 +1,117 @@
#include <stdio.h>
#include "include/ast.h"
#include "include/dstr.h"
#include "include/util.h"
extern AST* root;
static char* asttype_names[] = {
[AST_TYPE_CALL] = "FUNC CALL",
[AST_TYPE_NUM] = "NUMBER",
[AST_TYPE_VREF] = "VAR REFERENCE"
};
AST* ast_init(ASTType type, void* data) {
AST* ast = malloc(sizeof(AST));
ast->type = type;
ast->data = data;
return ast;
}
void ast_destroy(AST* ast) {
if (!ast) return;
switch (ast->type) {
case AST_TYPE_NUM: ast_num_data_destroy(ast->data); break;
case AST_TYPE_CALL: ast_call_data_destroy(ast->data); break;
default:
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_i(AST* ast, int i) {
INDENT_BEGIN(i);
INDENT_TITLE("AST", ast);
INDENT_FIELD("type", "%s", asttype_names[ast->type]);
INDENT_FIELD_EXT_NONL_START("data");
switch (ast->type) {
case AST_TYPE_NUM:
printf("%s %lf\n", INDENT_spacing->buf, *(ASTNumData*)ast->data);
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);
}
INDENT_FIELD_NONL_END;
INDENT_END;
}
ASTNumData* ast_num_data_init(double val) {
talloc(ASTNumData, num);
*num = val;
return num;
}
void ast_num_data_destroy(ASTNumData* num) { free(num); }
void ast_num_print(ASTNumData* data, int i) {
INDENT_BEGIN(i);
INDENT_FIELD("data", "%lf", *data);
INDENT_END;
}
ASTCallData* ast_call_data_init(char* to, size_t argc, AST** argv) {
talloc(ASTCallData, call);
log_dbgf("to: %s", to);
call->to = to;
call->argc = argc;
call->argv = argv;
return call;
}
void ast_call_data_destroy(ASTCallData* call) {
if (!call) return;
free(call->to);
for (size_t i = 0; i < call->argc; i++) ast_destroy(call->argv[i]);
free(call->argv);
free(call);
}
void ast_call_print(ASTCallData* data, int i) {
INDENT_BEGIN(i);
INDENT_TITLE("ASTCallData", data);
INDENT_FIELD("to", "%s", data->to);
INDENT_FIELD("argc", "%ld", data->argc);
INDENT_FIELD_LIST("argv", data->argv, data->argc, ast_print_i);
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;
}

54
src/dstr.c Normal file
View File

@@ -0,0 +1,54 @@
#include "include/dstr.h"
#include "include/util.h"
#include <stddef.h>
#include <stdio.h>
#include <string.h>
Dstr* dstr_init(void) {
Dstr* dstr = malloc(sizeof(Dstr));
dstr->sz = DSTR_INITSZ;
dstr->buf = malloc(DSTR_INITSZ);
*dstr->buf = '\0';
dstr->ln = 0;
return dstr;
}
void dstr_destroy(Dstr* dstr) {
free(dstr->buf);
free(dstr);
}
void dstr_destroypsv(Dstr* dstr) { free(dstr); }
// Check whether the buffer is overflowing and resize it if necessary.
void check_resz(Dstr* dstr, size_t ln) {
while (dstr->ln + ln + 1 > dstr->sz) {
// Double the buffer size when overflown.
dstr->sz *= 2;
dstr->buf = realloc(dstr->buf, dstr->sz);
log_dbgf(
"dstr @ %p doubled from %ld to %ld", dstr, dstr->sz / 2, dstr->sz
);
}
}
void dstr_append(Dstr* dest, char* src, size_t ln) {
check_resz(dest, ln);
// Overwrites the \0 at the end of the string, keeps the null from the given
// string.
memcpy(dest->buf + dest->ln, src, ln + 1);
dest->ln += ln;
}
void dstr_appendch(Dstr* dest, char ch) {
check_resz(dest, 1);
// Overwrites the preexisting null terminator, and adds one of its own.
dest->buf[dest->ln] = ch;
dest->buf[dest->ln + 1] = '\0';
dest->ln += 1;
}

70
src/exec.c Normal file
View File

@@ -0,0 +1,70 @@
#include <stddef.h>
#include <stdio.h>
#include <string.h>
#include "include/ast.h"
#include "include/exec.h"
#include "include/util.h"
extern AST* root;
ASTNumData exec_exp(AST* ast) {
log_dbg("Started execution.");
switch (ast->type) {
case AST_TYPE_CALL: return exec_call(ast);
case AST_TYPE_NUM: return *(ASTNumData*)ast->data;
default: printf("what\n");
exit(1);
}
}
ASTNumData exec_call(AST* ast) {
log_dbg("Started call execution.");
fflush(stdout);
ASTCallData* calldata = (ASTCallData*)ast->data;
if (calldata->argc >= 1) {
if (!strcmp(calldata->to, "sum")) {
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, "sub")) {
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, "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;
}
void exec_print(double n) { printf("= %lf\n", n); }

131
src/grammar.y Normal file
View File

@@ -0,0 +1,131 @@
%{
#include <string.h>
#include <stdio.h>
#include "../../src/include/ast.h"
#include "../../src/include/lexer.h"
int yylex(void);
void yyerror(char const*);
AST* root = NULL;
%}
%code requires {
#include "../../src/include/ast.h"
}
%union {
double fval;
char* strval;
AST* ast;
ArgArr* argarr;
}
%define parse.error verbose
%token LGROUP
%token RGROUP
%token SEP
%token<strval> WORD
%token<fval> NUM
%token SUB
%token PLUS
%token MULT
%token DIV
%token NL
%left PLUS SUB
%left MULT DIV
%precedence NEG
%type<ast> exp;
%type<argarr> arg;
%type<argarr> argstart;
%%
input:
%empty
| exp { root = $1; }
;
argstart:
exp {
ArgArr* argarr = argarr_init();
argarr_add(argarr, $1);
$$ = argarr;
}
;
arg:
argstart { $$ = $1; }
| arg SEP exp {
argarr_add($1, $3);
$$ = $1;
}
;
exp:
NUM { $$ = ast_init(AST_TYPE_NUM, ast_num_data_init($1)); }
| SUB exp {
AST** argv = calloc(2, sizeof(AST*));
argv[0] = ast_init(AST_TYPE_NUM, ast_num_data_init(-1));
argv[1] = $2;
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 {
AST** argv = calloc(2, sizeof(AST*));
argv[0] = $1;
argv[1] = $3;
char* to = malloc(4);
strcpy(to, "sum");
$$ = ast_init(AST_TYPE_CALL, ast_call_data_init(to, 2, argv));
}
| exp SUB exp {
AST** argv = calloc(2, sizeof(AST*));
argv[0] = $1;
argv[1] = $3;
char* to = malloc(4);
strcpy(to, "sub");
$$ = ast_init(AST_TYPE_CALL, ast_call_data_init(to, 2, argv));
}
| exp MULT exp {
AST** argv = calloc(2, sizeof(AST*));
argv[0] = $1;
argv[1] = $3;
char* to = malloc(4);
strcpy(to, "mul");
$$ = ast_init(AST_TYPE_CALL, ast_call_data_init(to, 2, argv));
}
| exp DIV exp {
AST** argv = calloc(2, sizeof(AST*));
argv[0] = $1;
argv[1] = $3;
char* to = malloc(4);
strcpy(to, "div");
$$ = ast_init(AST_TYPE_CALL, ast_call_data_init(to, 2, argv));
}
%%

47
src/include/ast.h Normal file
View File

@@ -0,0 +1,47 @@
#ifndef AST_H
#define AST_H
#include <stdlib.h>
typedef enum {
AST_TYPE_NUM, // A number.
AST_TYPE_CALL, // A function call.
AST_TYPE_VREF, // A variable reference.
AST_TYPE_MAX = AST_TYPE_CALL
} ASTType;
typedef struct {
ASTType type;
void* data;
} AST;
AST* ast_init(ASTType type, void* data);
void ast_destroy(AST* ast);
void ast_print(AST* ast);
void ast_print_i(AST* ast, int i);
typedef double ASTNumData;
ASTNumData* ast_num_data_init(double val);
void ast_num_data_destroy(ASTNumData* num);
void ast_num_print(ASTNumData*, int i);
typedef struct {
char* to; // What the call's to.
size_t argc; // Argument count.
AST** argv; // Argument vector.
} ASTCallData;
ASTCallData* ast_call_data_init(char* to, size_t argc, AST** argv);
void ast_call_data_destroy(ASTCallData* call);
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

25
src/include/dstr.h Normal file
View File

@@ -0,0 +1,25 @@
#ifndef DSTR_H
#define DSTR_H
#include <stdlib.h>
#define DSTR_INITSZ 2
typedef struct {
char* buf; // The buffer containing the string.
size_t sz; // The size of the buffer.
size_t ln; // The number of characters in the buffer.
} Dstr;
Dstr* dstr_init(void);
void dstr_destroy(Dstr* dstr);
// Destroy Dstr structure but preserve ->buf.
void dstr_destroypsv(Dstr* dstr);
// Append ln characters of src to dest.
void dstr_append(Dstr* dest, char* src, size_t ln);
// Append ch to dest.
void dstr_appendch(Dstr* dest, char ch);
#endif

10
src/include/exec.h Normal file
View File

@@ -0,0 +1,10 @@
#ifndef EXEC_H
#define EXEC_H
#include "ast.h"
ASTNumData exec_exp(AST* ast);
ASTNumData exec_call(AST* ast);
void exec_print(double n);
#endif

39
src/include/lexer.h Normal file
View File

@@ -0,0 +1,39 @@
#ifndef LEXER_H
#define LEXER_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"
extern YYSTYPE yylval;
// Accumulate an integer.
int acc_int(int c);
// Accumulate a floating-point number.
double acc_float(int c);
// Called by `yyparse()` (in bison-generated files.)
int yylex();
void yyerror(char const* s);
#endif

View File

@@ -1,6 +1,2 @@
#ifndef MAIN_H
#define MAIN_H
#include <stdio.h>
#endif
// This file serves no purpose but because I've written my makefile this way it
// has to exist for things to compile :P. TODO: Fix this.

View File

@@ -1,30 +0,0 @@
#ifndef PARSER_H
#define PARSER_H
// Expression one of:
// - Operation
// - Number
// Operation contains:
// - Type
// - Expression 1
// - Expression 2
typedef enum OpType {
OPTYPE_PLUS,
OPTYPE_MINUS
} optype_t;
typedef union Exp {
typedef struct Op {
optype_t type;
Exp* exp1;
Exp* exp2;
} op_t;
int n;
} exp_t;
#endif

23
src/include/stack.h Normal file
View File

@@ -0,0 +1,23 @@
#ifndef STACK_H
#define STACK_H
#include <stdlib.h>
#define STACK_MAX 64
typedef struct {
size_t i; // Current index in the stack.
void* val[STACK_MAX]; // The stack itself.
} Stack;
Stack* stack_init();
// Destroy a stack.
// Note that `stack->i` must be `0`.
void stack_destroy(Stack* stack);
// Push a value to the stack.
void stack_push(Stack* stack, void* val);
// Pop a value from the stack.
void* stack_pop(Stack* stack);
#endif

View File

@@ -1,12 +0,0 @@
#ifndef UTIL_H
#define UTIL_H
// Utilies.
#include <stdlib.h>
#include <stdio.h>
// Exit with an error. Returns int for ease of use, but should be treated as void.
int die(char* msg);
#endif

85
src/include/util.h Normal file
View File

@@ -0,0 +1,85 @@
#ifndef UTIL_H
#define UTIL_H
#ifdef DBG // Debug macros
// Log a message.
#define log_dbg(msg) \
printf("\033[37;1mdbg\033[0m:\033[37m%s\033[0m:\033[32m " msg "\033[0m\n", \
__func__);
// Log a message with formatting.
#define log_dbgf(msg, ...) \
printf("\033[37;1mdbg\033[0m:\033[37m%s\033[0m:\033[32m " msg "\033[0m\n", \
__func__, __VA_ARGS__);
#else // ifdef DBG
#define log_dbg(msg)
#define log_dbgf(msg, ...)
#endif // ifdef DBG else
// Resent color code.
#define COL_RESET "\e[0m"
// Regular color codes.
#define COL_BLA "\e[0;30m"
#define COL_RED "\e[0;31m"
#define COL_GRE "\e[0;32m"
#define COL_YEL "\e[0;33m"
#define COL_BLU "\e[0;34m"
#define COL_MAG "\e[0;35m"
#define COL_CYA "\e[0;36m"
#define COL_WHI "\e[0;37m"
// Bold color codes.
#define COL_BBLA "\e[1;30m"
#define COL_BRED "\e[1;31m"
#define COL_BGRE "\e[1;32m"
#define COL_BYEL "\e[1;33m"
#define COL_BBLU "\e[1;34m"
#define COL_BMAG "\e[1;35m"
#define COL_BCYA "\e[1;36m"
#define COL_BWHI "\e[1;37m"
// Start in indent block.
#define INDENT_BEGIN(ILVL) \
__attribute__((unused)) int INDENT_lvl = ILVL; \
Dstr* INDENT_spacing = dstr_init(); \
for (int INDENT_j = 0; INDENT_j < ILVL; INDENT_j++) \
dstr_appendch(INDENT_spacing, ' ');
// Print & indent the title of a section.
#define INDENT_TITLE(THING, WHERE) \
printf("%s" COL_BCYA THING COL_RESET " @" COL_MAG " %p\n" COL_RESET, INDENT_spacing->buf, WHERE);
// Print & indent a thing.
#define INDENT_FIELD(FIELD, VAL, ...) \
printf("%s " COL_BWHI FIELD ": " COL_RESET COL_WHI VAL COL_RESET "\n", \
INDENT_spacing->buf, __VA_ARGS__);
// Print & indent a thing with a newline before the val.
#define INDENT_FIELD_NL(FIELD, VAL, ...) \
printf("%s " COL_BWHI FIELD ":" COL_RESET "\n %s " COL_WHI VAL COL_RESET \
"\n", \
INDENT_spacing->buf, INDENT_spacing->buf, __VA_ARGS__);
// Print & indent a thing without any newline.
#define INDENT_FIELD_EXT_NONL_START(FIELD) \
printf("%s " COL_BWHI FIELD ":\n" COL_RESET COL_WHI, INDENT_spacing->buf);
#define INDENT_FIELD_NONL_END printf( "\n" COL_RESET);
// Print an array A of N things, by calling the function F.
#define INDENT_FIELD_LIST(FIELD, A, N, F) \
printf("%s " COL_BWHI FIELD ": [\n" COL_RESET, INDENT_spacing->buf); \
for (int INDENT_i = 0; INDENT_i < N; INDENT_i++) { \
F(A[INDENT_i], INDENT_lvl + 2); \
} \
printf(COL_BWHI "%s ]\n" COL_RESET, INDENT_spacing->buf);
// End an indent block.
#define INDENT_END printf(COL_RESET); dstr_destroy(INDENT_spacing);
// Allocate a pointer with a type.
#define talloc(T, X) T* X = malloc(sizeof(T));
#endif

120
src/lexer.c Normal file
View File

@@ -0,0 +1,120 @@
#include <complex.h>
#include <ctype.h>
#include <limits.h>
#include <math.h>
#include <stdio.h>
#include "include/dstr.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 value = c - '0';
while (isdigit(*inp)) {
value = value * 10 + (*inp - '0'); // Accumulate value.
inp++;
}
return value;
}
double acc_float(int c) {
int dplaces = 0;
double value = (double)(c - '0');
// Grab everything prior to '.'.
while (isdigit(*inp)) {
value = value * 10 + (*inp - '0'); // Accumulate value.
inp++;
}
if (*inp == '.') {
inp++;
while (isdigit(*inp)) {
value = value * 10 + (*inp - '0'); // Accumulate value.
dplaces++;
inp++;
}
value = value / pow(10, dplaces);
}
return value;
}
char* acc_word(int c) {
Dstr* val = dstr_init();
do {
dstr_appendch(val, *(inp - 1));
inp++;
} while (isalpha(*inp));
dstr_appendch(val, *(inp - 1));
char* ret = val->buf;
dstr_destroypsv(val);
return ret;
}
int yylex() {
if (*inp == '\0') return YYEOF;
// Skip all whitespace.
while (*inp == ' ' || *inp == '\t') inp++;
// Assign & consume current character.
int c = *inp++;
// Check for NUM.
if (isdigit(c)) {
yylval.fval = acc_float(c); // Set the token value.
return NUM;
}
if (isalpha(c)) {
yylval.strval = acc_word(c);
return WORD;
}
switch (c) {
case '+': return PLUS;
case '\n': return NL;
case '-': return SUB;
case '*': return MULT;
case '/': return DIV;
case '(': return LGROUP;
case ')': return RGROUP;
case ',': return SEP;
default: fprintf(stderr, "Unexpected character: %c\n", c);
}
return 0;
}
void yyerror(char const* s) { fprintf(stderr, "Parse error: %s\n", s); }

View File

@@ -1,6 +1,68 @@
#include <stdio.h>
#include <string.h>
#include "include/ast.h"
#include "include/dstr.h"
#include "include/exec.h"
#include "include/lexer.h"
#include "include/util.h"
#include "../build/grammars/grammar.tab.h"
// Global Abstract Syntax Tree.
extern AST* root;
// Global input text.
char* inp = NULL;
extern int yyparse();
int main(int argc, char** argv) {
printf("Hello, world!\n");
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) {
Dstr* ln = dstr_init();
char c;
printf("> ");
fflush(stdout);
// Accumulate line.
do {
c = getc(stdin);
switch (c) {
case EOF: dstr_destroy(ln); goto lnskip;
case '\n': goto lnend;
default: dstr_appendch(ln, c); log_dbgf("cchar: %c", c);
}
} while (1);
lnend:
log_dbgf("cline: %s", ln->buf);
if (ln->ln > 0) {
inp = ln->buf;
if (yyparse() == 0) {
log_dbg("Parsed successfully!\n");
} else printf("Parse error.\n");
exec_print(exec_exp(root));
#ifdef DBG
ast_print(root);
#endif
ast_destroy(root);
}
dstr_destroy(ln);
}
lnskip:;
return 0;
}

40
src/stack.c Normal file
View File

@@ -0,0 +1,40 @@
#include <assert.h>
#include <stdio.h>
#include <string.h>
#include "include/stack.h"
#include "include/util.h"
Stack* stack_init() {
talloc(Stack, stack);
memset(stack->val, 0, sizeof(void*) * STACK_MAX);
stack->i = 0;
return stack;
}
void stack_destroy(Stack* stack) {
// Can only free an empty stack.
assert(stack->i == 0);
free(stack);
}
void stack_push(Stack* stack, void* val) {
if (stack->i >= STACK_MAX) {
log_dbgf("Ran out of stack (max: %d)", STACK_MAX);
return;
}
stack->val[stack->i] = val;
stack->i++;
}
void* stack_pop(Stack* stack) {
if (stack->i <= 0) {
log_dbg("Can't pop empty stack.");
return (void*)-1;
}
return stack->val[--stack->i];
}

1
src/util.c Normal file
View File

@@ -0,0 +1 @@
#include "include/util.h"

1
test/Unity Submodule

Submodule test/Unity added at 73237c5d22

79
test/test_dstr.c Normal file
View File

@@ -0,0 +1,79 @@
#include "../src/include/dstr.h"
#include "Unity/src/unity.h"
#include <string.h>
void setUp() {};
void tearDown() {};
void test_dstr_init() {
Dstr* dstr = dstr_init();
TEST_ASSERT_EQUAL(0, strlen(dstr->buf));
TEST_ASSERT_EQUAL(0, dstr->ln);
TEST_ASSERT_EQUAL(DSTR_INITSZ, dstr->bufsz);
}
void test_dstr_append() {
Dstr* dstr;
// Test simple appending.
dstr = dstr_init();
char* hello_world = "Hello, world!";
dstr_append(dstr, hello_world, strlen(hello_world));
TEST_ASSERT_EQUAL_STRING(hello_world, dstr->buf);
TEST_ASSERT_EQUAL(strlen(hello_world), dstr->ln);
TEST_ASSERT_EQUAL(DSTR_INITSZ, dstr->bufsz);
dstr_destroy(dstr);
// Test buffer doubling.
dstr = dstr_init();
char h[DSTR_INITSZ + 20];
memset(h, 'h', DSTR_INITSZ + 19);
h[DSTR_INITSZ + 19] = '\0';
dstr_append(dstr, h, strlen(h));
TEST_ASSERT_EQUAL_STRING(h, dstr->buf);
TEST_ASSERT_EQUAL(strlen(h), dstr->ln);
TEST_ASSERT_EQUAL(DSTR_INITSZ * 2, dstr->bufsz);
}
void test_dstr_appendch() {
Dstr* dstr;
// Test simple character appending.
dstr = dstr_init();
char c = 'c';
char* c_str = "c";
dstr_appendch(dstr, c);
TEST_ASSERT_EQUAL_STRING(c_str, dstr->buf);
TEST_ASSERT_EQUAL(strlen(c_str), dstr->ln);
TEST_ASSERT_EQUAL(DSTR_INITSZ, dstr->bufsz);
dstr_destroy(dstr);
// Test buffer doubling.
dstr = dstr_init();
// Test against this string.
char h[DSTR_INITSZ + 20];
memset(h, 'h', DSTR_INITSZ + 19);
h[DSTR_INITSZ + 19] = '\0';
for (int i = 0; i < DSTR_INITSZ + 19; i++) dstr_appendch(dstr, 'h');
TEST_ASSERT_EQUAL_STRING(h, dstr->buf);
TEST_ASSERT_EQUAL(strlen(h), dstr->ln);
TEST_ASSERT_EQUAL(DSTR_INITSZ * 2, dstr->bufsz);
}
// not needed when using generate_test_runner.rb
int main(void) {
UNITY_BEGIN();
RUN_TEST(test_dstr_init);
RUN_TEST(test_dstr_append);
RUN_TEST(test_dstr_appendch);
return UNITY_END();
}

84
test/validation/test.bats Normal file
View 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" ]
}