init
This commit is contained in:
commit
1f0a783560
9 changed files with 1045 additions and 0 deletions
119
grammar/grammar.peg
Normal file
119
grammar/grammar.peg
Normal file
|
|
@ -0,0 +1,119 @@
|
|||
# vim:fileencoding=utf-8:foldmethod=marker
|
||||
|
||||
Program <- skip (Function _endline*)+ eof
|
||||
|
||||
# *** variable definition *** {{{
|
||||
|
||||
Variable <- IDENTIFIER Definition
|
||||
/ return_keyword " is " value
|
||||
|
||||
Definition <- " is " (value / "a " type)
|
||||
|
||||
value <- IDENTIFIER
|
||||
/ number
|
||||
|
||||
|
||||
type <- "number"
|
||||
|
||||
# }}}
|
||||
|
||||
fn_keyword <- "how-to"
|
||||
|
||||
argouments_keyword <- "requires"
|
||||
|
||||
return_keyword <- "resoult"
|
||||
|
||||
function_keywords <- fn_keyword / argouments_keyword / return_keyword
|
||||
|
||||
Function <-
|
||||
fn_keyword " " IDENTIFIER ":" newline
|
||||
Requires endline
|
||||
Resoult endline
|
||||
(Step endline)+
|
||||
|
||||
Requires <- argouments_keyword " " Argouments
|
||||
|
||||
Argouments <- "nothing"
|
||||
/ "a " type " " IDENTIFIER (" and a " type " " IDENTIFIER)*
|
||||
|
||||
Resoult <- return_keyword " is " ("nothing" / "a " type)
|
||||
|
||||
Fn_call <- IDENTIFIER " " (value (" and " value)*)? (steps_keyword " " number"-"number (" and " number"-"number)*)?
|
||||
|
||||
if_keyword <- "if"
|
||||
|
||||
If_call <- if_keyword " " value ((" and " value)* " are " / " is ") operation " " (Fn_call / Variable)
|
||||
|
||||
operation <- "equal"
|
||||
/ "different"
|
||||
/ "true"
|
||||
/ "false"
|
||||
|
||||
if_keywords <- if_keyword / operation
|
||||
|
||||
repeat_keyword <- "repeat"
|
||||
|
||||
step_keyword <- "step" # single step
|
||||
|
||||
steps_keyword <- "steps" # multiple steps
|
||||
|
||||
relative_keyword <- "from" # from x step to the end/current positionx
|
||||
|
||||
relative_position <- "the start"
|
||||
|
||||
relative_numbering_keyword <- "steps ago"
|
||||
|
||||
relative_numbering <- number " " relative_numbering_keyword
|
||||
|
||||
Repeat_call <- repeat_keyword " " (
|
||||
step_keyword " " number
|
||||
/ steps_keyword " " number"-"number ("and" number"-"number)*
|
||||
/ relative_keyword " " (
|
||||
relative_position
|
||||
/ relative_numbering
|
||||
/ step_keyword " " number
|
||||
)
|
||||
) (" with " value (" and " value)*)?
|
||||
|
||||
repeat_call_keywords <- repeat_keyword
|
||||
/ step_keyword
|
||||
/ steps_keyword
|
||||
/ relative_keyword
|
||||
/ relative_position
|
||||
/ relative_numbering_keyword
|
||||
|
||||
Step <- number") "
|
||||
(Variable
|
||||
/ If_call
|
||||
/ Repeat_call
|
||||
/ Fn_call) endline
|
||||
|
||||
IDENTIFIER <- !keyword [A-Za-z] [A-Za-z0-9_]*
|
||||
|
||||
generic_keywords <- "a "
|
||||
/ "is "
|
||||
/ "and "
|
||||
/ "nothing "
|
||||
keyword <- function_keywords / if_keywords / repeat_call_keywords / generic_keywords
|
||||
|
||||
comment <- "//" (!newline .)* newline
|
||||
# *** Nicities *** {{{
|
||||
|
||||
skip <- ([ \n] / comment)*
|
||||
|
||||
endline <- &. " "* comment? "\n"?
|
||||
_endline <- &. " "* comment? "\n"
|
||||
|
||||
eof <- !.
|
||||
|
||||
newline <- "\n"
|
||||
|
||||
number <- binary
|
||||
/ hex
|
||||
/ dec
|
||||
|
||||
binary <- "0b" [01]+
|
||||
hex <- "0x" [0-9A-Fa-f]+
|
||||
dec <- [0-9]+
|
||||
|
||||
# }}}
|
||||
42
grammar/parser.c
Normal file
42
grammar/parser.c
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
#include <stdio.h>
|
||||
|
||||
#define false 0
|
||||
#define true 1
|
||||
_Bool reached_EOF = false;
|
||||
#define YY_INPUT(buf, result, max_size) \
|
||||
{ \
|
||||
int yyc = getchar(); \
|
||||
if (yyc == EOF) { \
|
||||
reached_EOF = true; \
|
||||
fprintf(stdout, "EOF"); \
|
||||
} else { \
|
||||
fprintf(stdout, "%c", yyc); \
|
||||
} \
|
||||
result = (EOF == yyc) ? 0 : (*(buf) = yyc, 1); \
|
||||
}
|
||||
|
||||
#include "peg.c"
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
if (argc < 2) {
|
||||
fprintf(stderr, "usage: %s filen.fang [output/file]\n", *argv);
|
||||
return -1;
|
||||
}
|
||||
freopen(argv[1], "r", stdin);
|
||||
if (!stdin) {
|
||||
fprintf(stderr, "file %s not found\n", argv[1]);
|
||||
return -2;
|
||||
}
|
||||
_Bool newline = true;
|
||||
if (argc >= 3) {
|
||||
freopen(argv[2], "w", stdout);
|
||||
if (!stdout) {
|
||||
fprintf(stderr, "file %s not found\n", argv[2]);
|
||||
return -2;
|
||||
}
|
||||
newline = false;
|
||||
}
|
||||
while (yyparse());
|
||||
if (newline) printf("\n");
|
||||
return !reached_EOF;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue