ref: c307d63f28ceab7201fb9aeb770edfb7d91b5055
parent: 0841450d6cd36d0799205a165b6f829e3fd7bb9e
author: Ori Bernstein <ori@eigenstate.org>
date: Wed Feb 1 18:04:01 EST 2017
Update the grammar.
--- a/doc/lang.txt
+++ b/doc/lang.txt
@@ -1,6 +1,6 @@
The Myrddin Programming Language
Jul 2012
- Updated Dec 2016
+ Updated Feb 2017
Ori Bernstein
TABLE OF CONTENTS:
@@ -140,7 +140,7 @@
3.2. Top Level Structure:
- file: (decl | package | use | implstmt | traitdef | tydef)*
+ file: (decl | package | use | impldef | traitdef | tydef)*
A file is composed of a sequence of top level elements. These
top level elements consist of:
@@ -564,7 +564,7 @@
4.4.2. Impls:
- implstmt: "impl" ident imptypes "=" implbody
+ impldef: "impl" ident imptypes "=" implbody
traittypes: type ["->" type ("," type)*]
traitbody: (name [":" type] "=" expr)*
@@ -1868,6 +1868,119 @@
goto: goto ident
6. GRAMMAR:
+
+ /* top level */
+ file: toplev*
+ toplev: use | pkgdef | decl | traitdef | impldef | tydef
+
+ /* packages */
+ use: use ident | strlit
+ pkgdef: "pkg" [ident] "=" pkgbody ";;"
+ pkgbody: (decl | attrs tydef | traitdef | impldef)*
+
+ /* declarations */
+ decl: attrs ("var" | "const") decllist
+ attrs: ("$noret" | "extern" | "pkglocal")*
+ decllist: declbody ("," declbody)+
+ declbody: declcore ["=" expr]
+ declcore: ident [":" type]
+
+ /* traits */
+ traitdef: "trait" ident typaram [auxtypes] ("\n" | "=" traitbody ";;")
+ auxtypes: "->" type ("," type)*
+ traitbody: "\n"* (ident ":" type "\n"*)*
+ impldef: "impl" ident type [auxtypes] ("\n" | "=" implbody ";;")
+ implbody: "\n"* (ident [":" type] "=" expr "\n"*)*
+
+ /* types */
+ tydef: "type" typeid "=" type
+ typeid: ident | ident "(" typarams ")"
+ typarams: typaram ("," typaram)*
+ type: structdef | uniondef | tupledef |
+ compound | generic | "..."
+ structdef: "struct" structbody ";;"
+ uniondef: "union" unionbody ";;"
+ tupledef: "(" type ("," type)* ")"
+ generic: typaram ["::" traitlist]
+ traitlist: name | "(" name ("," name)
+ compound: functype | sicetype | arraytype | ptrtype | void | name
+ functype: "(" arglist "->" type ")"
+ arglist: [arg ("," arg)*]
+ arg: name ":" type
+ slicetype: type "[" ":" "]"
+ arraytype: type "[" (expr | "...") "]"
+ ptrtype: type "#"
+ void: "void"
+
+ /* expressions */
+ retexpr: "->" expr | expr
+ exprln: expr ";"
+ expr: lorepxr asnop expr | lorexpr
+ lorexpr: lorexpr "||" landexpr | landexpr
+ landexpr: landexpr "&&" cmpexpr | cmpexpr
+ cmpexpr: cmpexpr ("<" | "<=" | "==" | ">=" | ">") unionexpr | unionexpr
+ unionexpr: "`" name unionexpr | "`" name | borexpr
+ borexpr: borexpr ("|" | "^" ) bandexpr | bandexpr
+ bandexpr: bandexpr "&" addexpr | addexpr
+ addexpr: addexpr ("+" | "-") mulexpr
+ mulexpr: mulexpr ("*" | "/" | "%") shiftexpr
+ shiftexpr: shiftexpr ("<<" | ">>") prefixexpr
+ preexpr: "++" prefixexpr | "--" prefixexpr |
+ "!" prefixexpr | "~" prefixexpr |
+ "-" prefixexpr | "+" prefixexpr |
+ "&" prefixexpr | postexpr
+ postexpr: postexpr "." ident |
+ postexpr "++" | postexpr "--" |
+ postexpr "[" expr "]" |
+ postexpr "[" [expr] ":" [expr] "]" |
+ postepxr "#" |
+ atomicexpr
+ atomicexpr: ident | literal | "(" expr ")" | "sizeof" "(" type ")"
+
+ /* literals */
+ literal: funclit | seqlit | tuplit | simplelit
+ funclit: "{" [funcargs] ";" blockbody "}"
+ funcargs: ident [ ":" type] ("," ident [ ":" type])*
+ seqlit: "[" structelts | [arrayelts] "]"
+ arrayelts: arrayelt ("," arrayelt)*
+ arrayelt: ";'* expr [":" expr] ";"*
+ structelts: structelt ("," ";"* structelt)*
+ structelt: ";"* "." name "=" expr ";"*
+ tuplit: "(" expr "," [expr ("," expr)*] ")"
+ simplelit: strlist | chrlit | fltlit | boollit | voidlit | intlit
+ fltlit: <float literal>
+ boollit: "true" | "false"
+ voidlit: "void"
+ strlit: <string literal>
+ chrlit: <char literal>
+
+ /* statements */
+ blkbody: (decl | stmt | tydef | ";")*
+ stmt: jmpstmt | flowstmt | retexpr
+ jmpstmt: goto | break | continue | label
+ flowstmt: ifstmt | forstmt | whilestmt | matchstmt
+
+ ifstmt: "if" exprln blkbody elifs ["else" blkbody] ";;"
+ elifs: ("elif" exprln blkbody)*
+ forstmt: foriter | forstep
+ foriter: "for" expr "in" exprln blkbody ";;"
+ forstep: "for" exprln exprln exprln blkbody ";;"
+ whilestmt: "while" exprln blkbody ";;"
+ matchstmt: "match" exprln pattern* ";;"
+ pattern: "|" expr ":" blkbody ";"
+ goto: "goto" ident ";"
+ break: "break" ";"
+ continue: "continue" ";"
+ label: ":" ident ";"
+
+ /* misc */
+ name: ident | ident "." ident
+ asnop: "=" | "+=" | "-=" | "*=" | "/=" | "%=" |
+ "|=" | "^=" | "&=" | "<<=" | ">>="
+
+ /* pattern tokens */
+ typaram: /@[a-zA-Z_][a-zA-Z0-9_]*/
+ ident: /[a-zA-Z_][a-zA-Z0-9_]*/
BUGS:
--- a/parse/gram.y
+++ b/parse/gram.y
@@ -78,7 +78,6 @@
%token<tok> Tcparen /* ) */
%token<tok> Tosqbrac /* [ */
%token<tok> Tcsqbrac /* ] */
-%token<tok> Tat /* @ */
%token<tok> Ttick /* ` */
%token<tok> Tderef /* # */
@@ -385,7 +384,7 @@
}
;
-traitdef: Ttrait Tident generictype optauxtypes { /* trait prototype */
+traitdef: Ttrait Tident generictype optauxtypes Tendln { /* trait prototype */
$$ = mktrait($1->loc,
mkname($2->loc, $2->id), $3,
$4.types, $4.ntypes,
@@ -498,7 +497,6 @@
| type Tosqbrac Tellipsis Tcsqbrac {$$ = mktyarray($2->loc, $1, NULL);}
| name Toparen typelist Tcparen {$$ = mktyunres($1->loc, $1, $3.types, $3.ntypes);}
| type Tderef {$$ = mktyptr($2->loc, $1);}
- | Tat Tident {$$ = mktyparam($1->loc, $2->id);}
| Tvoidlit {$$ = mktyunres($1->loc, mkname($1->loc, $1->id), NULL, 0);}
| name {$$ = mktyunres($1->loc, $1, NULL, 0);}
;
@@ -584,7 +582,7 @@
| Tendln {$$ = NULL;}
;
-goto : Tgoto Tident {
+goto : Tgoto Tident Tendln {
Node *lbl;
lbl = mklbl($2->loc, "");
@@ -901,11 +899,11 @@
| /* empty */ {$$ = NULL;}
;
-break : Tbreak
+break : Tbreak Tendln
{$$ = mkexpr($1->loc, Obreak, NULL);}
;
-continue : Tcontinue
+continue : Tcontinue Tendln
{$$ = mkexpr($1->loc, Ocontinue, NULL);}
;
@@ -1006,7 +1004,7 @@
}
;
-label : Tcolon Tident {
+label : Tcolon Tident Tendln {
char buf[512];
genlblstr(buf, sizeof buf, $2->id);
$$ = mklbl($2->loc, buf);