~evanj/zan

ade3c9ec49841b3cd86f9cf380abed0166b29b21 — Evan M Jones 10 months ago ce22bdf
Feat(flex+bison): Initial grammar work for bison.
5 files changed, 169 insertions(+), 301 deletions(-)

M .gitignore
M examples/api/client.z
M src/lex.l
A src/makefile
M src/yacc.y
M .gitignore => .gitignore +3 -0
@@ 1,2 1,5 @@
*.yy.c
*.tab.*
*.out
zan
src/test.z

M examples/api/client.z => examples/api/client.z +49 -0
@@ 80,3 80,52 @@ fn main [ string list args ] [
    fmt.print [ word ":\n" item.string [] ] 
  ]
]



pkg main

use [
  io 
]

fn main [] [ 
  io.print [ "hi" ]  
]






































M src/lex.l => src/lex.l +1 -0
@@ 34,6 34,7 @@ void count();
"next"  { count(); return KW_NEXT; }
"ptr"   { count(); return KW_PTR; }
"ref"   { count(); return KW_REF; }
"test"  { count(); return KW_TEST; }

{L}({L}|{D})*		{ count(); return(check_type()); }


A src/makefile => src/makefile +8 -0
@@ 0,0 1,8 @@
prog:
	flex lex.l && \
	bison -d yacc.y && \
	gcc -o zan lex.yy.c yacc.tab.c

clean: 
	rm lex.yy.c yacc.tab.c yacc.tab.h zan 


M src/yacc.y => src/yacc.y +108 -301
@@ 4,300 4,107 @@
%token BYTE SHORT INT LONG FLOAT DOUBLE SIGNED UNSIGNED TYPE_NAME
%token ELLIPSIS

%token KW_PKG KW_USE KW_FN KW_GIVE KW_RET KW_MATCH KW_BREAK KW_LOOP KW_TYPE KW_MAP KW_LIST KW_AND KW_OR KW_NOT KW_IF KW_ELSE KW_DEF KW_LATER KW_NEXT KW_PTR KW_REF
%token KW_PKG KW_USE KW_FN KW_GIVE KW_RET KW_MATCH KW_BREAK KW_LOOP KW_TYPE
%token KW_MAP KW_LIST KW_AND KW_OR KW_NOT KW_IF KW_ELSE KW_DEF KW_LATER 
%token KW_NEXT KW_PTR KW_REF KW_TEST

%start program

%%

primary_expression
	: ID
	| STRING_LIT
	| '[' expression ']'
	;

postfix_expression
	: primary_expression
	| postfix_expression '[' expression ']'
	| postfix_expression '[' ']'
	| postfix_expression '[' argument_expression_list ']'
	| postfix_expression '.' ID
	| postfix_expression INC_OP
	| postfix_expression DEC_OP
	;

argument_expression_list
	: assignment_expression
	| argument_expression_list ',' assignment_expression
	;

unary_expression
	: postfix_expression
	| INC_OP unary_expression
	| DEC_OP unary_expression
	| unary_operator unary_expression
	;

unary_operator
	: '*'
	| '/'
	| '+'
	| '-'
	;

multiplicative_expression
	: unary_expression
	| multiplicative_expression '*' unary_expression
	| multiplicative_expression '/' unary_expression
	| multiplicative_expression '%' unary_expression
	;

additive_expression
	: multiplicative_expression
	| additive_expression '+' multiplicative_expression
	| additive_expression '-' multiplicative_expression
	;

relational_expression
	: additive_expression
	| relational_expression '<' additive_expression
	| relational_expression '>' additive_expression
	| relational_expression LE_OP additive_expression
	| relational_expression GE_OP additive_expression
	;

equality_expression
	: relational_expression
	| equality_expression EQ_OP relational_expression
	| equality_expression NE_OP relational_expression
	;

logical_equality_expression
	: equality_expression
	| logical_equality_expression KW_AND equality_expression
	;

logical_or_expression
	: logical_equality_expression
	| logical_or_expression KW_OR logical_equality_expression
	;

assignment_expression
	: logical_or_expression
	| unary_expression assignment_operator assignment_expression
	;

assignment_operator
	: '='
	;

expression
	: assignment_expression
	| expression ',' assignment_expression
	;

constant_expression
	: logical_or_expression
	;

declaration
	: declaration_specifiers ';'
	| declaration_specifiers init_declarator_list ';'
	;

declaration_specifiers
	: type_specifier
	| type_specifier declaration_specifiers
	;

init_declarator_list
	: init_declarator
	| init_declarator_list ',' init_declarator
	;

init_declarator
	: declarator
	| declarator '=' initializer
	;

type_specifier
	: BYTE
	| SHORT
	| INT
	| LONG
	| FLOAT
	| DOUBLE
	| SIGNED
	| UNSIGNED
	| struct_specifier
	| TYPE_NAME
	;

struct_specifier
	: KW_TYPE ID '[' struct_declaration_list ']'
	| KW_TYPE '[' struct_declaration_list ']'
	| KW_TYPE ID
	;

struct_declaration_list
	: struct_declaration
	| struct_declaration_list struct_declaration
	;

struct_declaration
	: specifier_qualifier_list struct_declarator_list
	;

specifier_qualifier_list
	: type_specifier specifier_qualifier_list
	| type_specifier
	;

struct_declarator_list
	: struct_declarator
	| struct_declarator_list ',' struct_declarator
	;

struct_declarator
	: declarator
	| ':' constant_expression
	| declarator ':' constant_expression
	;

declarator
	: pointer direct_declarator
	| direct_declarator
	;

direct_declarator
	: ID
	| '[' declarator ']'
	| direct_declarator '[' constant_expression ']'
	| direct_declarator '[' ']'
	| direct_declarator '[' parameter_type_list ']'
	| direct_declarator '[' identifier_list ']'
	;

pointer
	: '*'
	| '*' pointer
	;

parameter_type_list
	: parameter_list
	| parameter_list ',' ELLIPSIS
	;

parameter_list
	: parameter_declaration
	| parameter_list ',' parameter_declaration
	;

parameter_declaration
	: declaration_specifiers declarator
	| declaration_specifiers abstract_declarator
	| declaration_specifiers
	;

identifier_list
	: ID
	| identifier_list ',' ID
	;
program
  : source_files
  ;

type_name
	: specifier_qualifier_list
	| specifier_qualifier_list abstract_declarator
	;

abstract_declarator
	: pointer
	| direct_abstract_declarator
	| pointer direct_abstract_declarator
	;

direct_abstract_declarator
	: '[' abstract_declarator ']'
	| '[' ']'
	| '[' constant_expression ']'
	| direct_abstract_declarator '[' ']'
	| direct_abstract_declarator '[' constant_expression ']'
	| '[' parameter_type_list ']'
	| direct_abstract_declarator '[' '['
	| direct_abstract_declarator '[' parameter_type_list ']'
	;

initializer
	: assignment_expression
	| '[' initializer_list ']'
	| '[' initializer_list ',' ']'
	;

initializer_list
	: initializer
	| initializer_list ',' initializer
	;

statement
	: labeled_statement
	| compound_statement
	| expression_statement
	| selection_statement
	| iteration_statement
	| jump_statement
	;

labeled_statement
	: constant_expression '[' statement ']'
	| KW_DEF ':' statement
	;

compound_statement
	: '[' ']'
	| '[' statement_list ']'
	| '[' declaration_list ']'
	| '[' declaration_list statement_list ']'
	;

declaration_list
	: declaration
	| declaration_list declaration
	;

statement_list
	: statement
	| statement_list statement
	;

expression_statement
	: ';'
	| expression ';'
	;

selection_statement
	: KW_IF expression '[' statement ']'
	| KW_IF expression '[' statement ']' KW_ELSE '[' statement ']'
	| KW_MATCH expression '[' statement ']'
	;

iteration_statement
	: KW_LOOP expression '[' statement ']'
	| KW_LOOP expression_statement expression_statement expression '[' statement ']'
	;

jump_statement
	: KW_NEXT
	| KW_BREAK
	| KW_RET
	| KW_RET expression
	;
source_files
  : source_files source_file
  | source_file
  ;

program
source_file
  : KW_PKG ID
  | KW_PKG ID translation_unit
  | KW_PKG ID use_declaration translation_unit
  | translation_unit
  | KW_PKG ID top_level_dec_list
  | KW_PKG ID use_dec top_level_dec_list
  | KW_PKG ID use_dec top_level_dec_list give_dec
  | KW_PKG ID top_level_dec_list give_dec
  ;

top_level_dec_list
  : top_level_dec_list top_level_dec
  | top_level_dec
  ;

top_level_dec
  : fun_dec
  | method_dec
  | type_dec
  | var_dec
  | test_dec
  ;

test_dec
  : KW_TEST ID '[' block ']'
  ;

fun_dec
  : KW_FN ID '[' ']' '[' block ']'
  | KW_FN ID '[' arg_list ']' '[' block ']'
  | KW_FN ID '[' ']' type_name '[' block ']'
  | KW_FN ID '[' arg_list ']' type_name '[' block ']'
  | KW_FN ID '[' ']' '[' ret_list ']' '[' block ']'
  | KW_FN ID '[' arg_list ']' '[' ret_list ']' '[' block ']'
  ;

method_dec
  : KW_FN type_name ID '[' ']' '[' block ']'
  | KW_FN type_name ID '[' arg_list ']' '[' block ']'
  | KW_FN type_name ID '[' ']' '[' ret_list ']' '[' block ']'
  | KW_FN type_name ID '[' arg_list ']' '[' ret_list ']' '[' block ']'
  ;

var_dec 
  : ID '=' exp
  ;

arg_list 
  : arg_list type_name ID
  | type_name ID
  ;

ret_list 
  : ret_list type_name
  | type_name
  ;

use_declaration
type_dec
  : KW_TYPE ID '[' arg_or_fun_dec_list ']'
  ;

arg_or_fun_dec_list
  : arg_or_fun_dec_list arg_or_fun_dec
  | arg_or_fun_dec
  ;

arg_or_fun_dec 
  : type_name ID
  | fun_dec
  ;

give_dec
  :  KW_GIVE '[' dot_id_list ']'
  ;

dot_id_list
  : dot_id_list dot_id
  | dot_id 
  ;

dot_id
  : dot_id '.' ID
  | ID
  ;

use_dec
  : KW_USE '[' package_name_list ']'
  ;



@@ 307,28 114,28 @@ package_name_list
  ;

package_name 
  : ID '/' ID
  | ID
  : dot_id_list '/' dot_id_list 
  | dot_id_list
  ;

type_name
  : ID
  ;

translation_unit
	: external_declaration
	| translation_unit external_declaration
	;
block
  : todo
  ;

external_declaration
	: function_definition
	| declaration
	;
exp
  : todo
  ;

function_definition
	: declaration_specifiers declarator declaration_list compound_statement
	| declaration_specifiers declarator compound_statement
	| declarator declaration_list compound_statement
	| declarator compound_statement
	;
todo
  : '.'
  ;

%%

#include <stdio.h>

extern char yytext[];