Include yacc in Makefile + misc

This commit is contained in:
attilavs2 2025-03-08 11:14:18 +01:00
parent be2c4b95df
commit 61290a18b9
4 changed files with 52 additions and 55 deletions

View file

@ -20,8 +20,9 @@ BUILD_DIR = build
SRC_DIR = src SRC_DIR = src
OBJS = $(patsubst $(SRC_DIR)/%.c,build-tmp/%.o,$(wildcard $(SRC_DIR)/*.c)) OBJS = $(patsubst $(SRC_DIR)/%.c,build-tmp/%.o,$(wildcard $(SRC_DIR)/*.c))
OBJS += build-tmp/y.tab.o build-tmp/lex.yy.o
all: | builddir build builddir2 all: | builddir yacc build builddir2
windef: windef:
$(eval OUTPUT = "$(OUTNAME).exe") $(eval OUTPUT = "$(OUTNAME).exe")
@ -35,6 +36,11 @@ builddir:
@- rm -rf $(wildcard build-tmp/*) @- rm -rf $(wildcard build-tmp/*)
@- mv $(wildcard $(BUILD_DIR)/*.o) build-tmp/ @- mv $(wildcard $(BUILD_DIR)/*.o) build-tmp/
yacc: src/parser.y src/parser.l
cd src && yacc -d -g parser.y && lex parser.l
$(CC) -c src/y.tab.c -o build-tmp/y.tab.o $(CFLAGS)
$(CC) -c src/lex.yy.c -o build-tmp/lex.yy.o $(CFLAGS)
build-tmp/%.o : $(SRC_DIR)/%.c build-tmp/%.o : $(SRC_DIR)/%.c
${CC} -c $< -o $@ ${CFLAGS} ${CC} -c $< -o $@ ${CFLAGS}
@ -59,6 +65,7 @@ clean:
- rm -rf build-win - rm -rf build-win
- rm -rf build-tmp - rm -rf build-tmp
- rm "$(OUTNAME).amd64" "$(OUTNAME).exe" - rm "$(OUTNAME).amd64" "$(OUTNAME).exe"
- rm src/lex.yy.c src/y.tab.c src/y.tab.h
.NOTPARALLEL: builddir builddir2 .NOTPARALLEL: builddir yacc builddir2
.PHONY: all test clean win windef builddir builddir2 testwin .PHONY: all test clean win windef builddir builddir2 testwin yacc

79
spec.md
View file

@ -1,38 +1,29 @@
# fLisp # fLisp
- S-expressions: In this document :
`(<function> [arg0 ... argN])` - `<...>` :
Will call function with optional args arg0-argN Mandatory argument
`([*]<variable> [arg | *arg] [arg2])` - `[...]` :
If arg is given, it will assign arg to variable, then return variable Optional argument
If assigning to a vector or string : - `|` :
- if variable is a string, arg may be a string literal. In this case, Or
variable becomes the string literal with or without `*` specified
- In the same way, if variable is a vector, arg may be a vector literal,
and the same rules apply
- If `*` is specified before variable, arg will replace the vector
- Otherwise, if only arg is given, it will return the element at position
arg of variable. If arg2 is given, it will set the element at position
arg of variable to arg2.
Examples:
```
fLisp : (let:vec:int a 5)
C : int a[5];
fLisp : (a 0 69) - Calls
C : a[0] = 69; `(<func> [arg0 ... argN])`
Calls func with optional args arg0 to argN
fLisp : (let:int x) - Assignements
(x (a 0)) `(<var> <value>)`
C : int x = a[0]; Will assign value to var. Value may be any valid expression
Vectors and strings :
fLisp : (let:str hello) - `(<vec> <pos>)` will return the value at position pos of vector vec.
(hello "Hello !") - `(<vec> <pos> <value>)` will assign value to position pos of vector vec
(*hello "Hello !") # equivalent - `(<string> <pos>)` will return a one length string, the character at pos
C : char *hello = "Hello !"; of string
``` - `(<string> <pos> <value>)` will assign the first char of string value to
For vectors and strings, it's a shallow copy (data is the same) unless a `*` position pos of the string
is specified before `[arg]` - `(*<vec|string> <value>)` will replace the contents of vec/string with a
shallow copy of value
- `(*<vec|string> *<value>)` Same as the previous, but deepcopies value
- Literals: - Literals:
Literals are of the following form: Literals are of the following form:
- `1234`: int literal - `1234`: int literal
@ -42,18 +33,9 @@
Null and fix literals do not exist. "Fix" type constants may exist by Null and fix literals do not exist. "Fix" type constants may exist by
assigning to a fix typed variable, such as `(let:fix x 0.5)`, where x will assigning to a fix typed variable, such as `(let:fix x 0.5)`, where x will
be of value 0.5 in fixed point be of value 0.5 in fixed point
- Comments are indicated by `#`. The whole rest of the line is ignored by the - Comments are indicated by `#`. The whole rest of the line is ignored
parser
- Compiled to bytecode - Compiled to bytecode
In this document :
- `<...>` :
Mandatory argument
- `[...]` :
Optional argument
- `|` :
Or
## Core types ## Core types
- `null`: - `null`:
@ -63,14 +45,13 @@ In this document :
- `fix`: - `fix`:
16:16 bit fixed point signed integer, same evaluation rules as ints 16:16 bit fixed point signed integer, same evaluation rules as ints
- `float`: - `float`:
32bit IEEE floating point value, same evaluation as ints (within 32bit IEEE floating point value, same evaluation as ints (within epsilon)
`FL_ESPILOǸ̀`)
- `str`: - `str`:
ASCII character string - always evaluates to false ASCII character string - always evaluates to false
- `fn`: - `fn`:
Function, function "variables" are inherently of type fn - handle with care ! Function, function "variables" are inherently of type fn - handle with care !
- `vec`: - `vec`:
Vectors of any type Vectors of any type, the syntax is `:vec:<type>`
## Core functions ## Core functions
@ -92,12 +73,12 @@ In this document :
- `(while <cond> [expressions])`: - `(while <cond> [expressions])`:
while cond evalutates to true, will execute optional expressions while cond evalutates to true, will execute optional expressions
- `(fn (<name> [arg0[:type] ... argN[:type]]) [expressions])` : - `(fn (<name>[:type] [arg0[:type] ... argN[:type]]) [expressions])` :
Declares a function of name name, optional args arg0-argN with optional Declares a function of name name, optional return type type,
types (recommended to explicit them), that executes optional expressions optional args arg0-argN with optional types (recommended to explicit them),
that executes expressions. There should at least be one expression.
The function will return the value of the last expression The function will return the value of the last expression
Passed vectors and strings are shallow copied Passed vectors and strings are shallow copied
The name cannot be a variable
- `(import <lib0> [... libN])` : - `(import <lib0> [... libN])` :
Import the functions and variables from lib0-libN Import the functions and variables from lib0-libN

View file

@ -12,6 +12,8 @@
ASTStack *stack; ASTStack *stack;
void yyerror(char *s);
int make_stack(){ int make_stack(){
stack = malloc(sizeof(ASTStack)+sizeof(Statement)*2048); stack = malloc(sizeof(ASTStack)+sizeof(Statement)*2048);
if(!stack) if(!stack)

View file

@ -45,6 +45,13 @@ type:
{Tag tag = get_type($2); tag.is_array = 1; $$ = tag;} {Tag tag = get_type($2); tag.is_array = 1; $$ = tag;}
; ;
fn_args:
G_IDENT type
{}
| fn_args G_IDENT type
{}
;
stmt: stmt:
'(' G_IDENT ')' '(' G_IDENT ')'
{$$ = make_operation(ST_Call, 0, $2, 0, NULL);} {$$ = make_operation(ST_Call, 0, $2, 0, NULL);}
@ -60,7 +67,7 @@ stmt:
} }
| '(' WHILE stmt stmt_list ')' | '(' WHILE stmt stmt_list ')'
{} {}
| '(' FN stmt stmt_list ')' | '(' FN fn_args stmt_list ')'
{} {}
| '(' GTEQ stmt stmt ')' | '(' GTEQ stmt stmt ')'
{} {}