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
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:
$(eval OUTPUT = "$(OUTNAME).exe")
@ -35,6 +36,11 @@ builddir:
@- rm -rf $(wildcard 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
${CC} -c $< -o $@ ${CFLAGS}
@ -59,6 +65,7 @@ clean:
- rm -rf build-win
- rm -rf build-tmp
- rm "$(OUTNAME).amd64" "$(OUTNAME).exe"
- rm src/lex.yy.c src/y.tab.c src/y.tab.h
.NOTPARALLEL: builddir builddir2
.PHONY: all test clean win windef builddir builddir2 testwin
.NOTPARALLEL: builddir yacc builddir2
.PHONY: all test clean win windef builddir builddir2 testwin yacc

83
spec.md
View file

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

View file

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

View file

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