Files
cval/README.md
2025-11-20 13:35:20 +09:00

101 lines
1.8 KiB
Markdown

# C-val Compiler
## Lexical Spec
* LBRACK `[`
* RBRACK `]`
* LCURLY `{`
* RCURLY `}`
* LPAREN `(`
* RPAREN `)`
* ID `[any character without whitespace]+`
* SEMI `;`
* COMMA `,`
* ARROW `->`
* STAR `*`
* ANDREF `&`
* DOLLAR `$`
* COMMENT `//`
* NUM `[0-9]*`
* VAL
* STRING `"{any}"`
## Syntax Spec
```spec
program := defn*
defn := VAL type ID ;
| VAL type ID expr ;
type := ID | type STAR
| LBRACK RBRACK | LBRACK type RBRACK
| LBRACK type* ARROW type? RBRACK
expr := atom atom*
atom := ID
| NUM
| STR
| lambda
| compound
| STAR
| ANDREF
stmt := defn // defn statement
| expr ; // expr statement
| return expr ; // return statement
| DOLLAR ID expr ; // assignment statement
| if expr compound ; // if statement
| if expr compound else compound ; // if-else statement
param_list := LPAREN (type ID)* RPAREN
lambda := param_list compound
compound := LCURLY (stmt)* expr? RCURLY
```
## AST Node Spec
```c
NODE_PROGRAM:
token: PROGRAM
children: NODE_DEFN*
NODE_DEFN:
token: VAL
children: NODE_TYPE, ID, NODE_EXPR?
NODE_TYPE:
token: ID | COMPLEX_TYPE
children: NODE_TYPE, NODE_TYPE
| NODE_TYPE
NODE_EXPR:
token: EXPR
children: NODE_ATOM*
// ATOM
NODE_NUM:
token: NUM
children: none
NODE_ID:
token: ID
children: none
NODE_STR:
token: STR
children: none
NODE_LAMBDA:
token: LAMBDA
children: NODE_PARAM_LIST, NODE_COMPOUND
NODE_PARAM_LIST:
token: PARAM_LIST
children: (NODE_PARAM)*
NODE_PARAM:
token: PARAM
children: NODE_TYPE, ID
NODE_COMPOUND:
token: COMPOUND
children: NODE_STMT*, NODE_EXPR?
```