Skip to content

Commit

Permalink
init parser
Browse files Browse the repository at this point in the history
  • Loading branch information
ShigekiKarita committed Dec 29, 2019
0 parents commit f703122
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 0 deletions.
15 changes: 15 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
.dub
docs.json
__dummy.html
docs/
/scheme-d
scheme-d.so
scheme-d.dylib
scheme-d.dll
scheme-d.a
scheme-d.lib
scheme-d-test-*
*.exe
*.o
*.obj
*.lst
13 changes: 13 additions & 0 deletions dub.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"authors": [
"karita"
],
"copyright": "Copyright © 2019, karita",
"dependencies": {
"pegged": "~>0.4.4",
"sumtype": "~>0.9.4"
},
"description": "Write yourself a Scheme in 48 Hours in D",
"license": "BSL-1.0",
"name": "scheme-d"
}
7 changes: 7 additions & 0 deletions dub.selections.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"fileVersion": 1,
"versions": {
"pegged": "0.4.4",
"sumtype": "0.9.4"
}
}
58 changes: 58 additions & 0 deletions source/app.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import std.stdio;
import std.typecons;

import sumtype;
import pegged.grammar;


mixin(grammar(`
SchemeParser:
Body < SExpr* / Comment
Comment <: ';' (!endOfLine .)* endOfLine
SExpr < :'(' (Value / Comment)* :')'
Value < Number / List / Atom / String / True / False / SExpr
List < quote SExpr
Atom <~ AtomChar (AtomChar / Digit)*
AtomChar <- [a-zA-Z_] / '+' / '-' / '*' / '/'
/ quote / ':' / '!' / '$' / '%' / '&' / '=' / '~' / '^' / ':'
String <~ :doublequote Char* :doublequote
Char <~ backslash doublequote
/ backslash backslash
/ backslash [bfnrt]
/ (!doublequote .)
Number <~ '-'? ('0' / [1-9] Digit* ('.' Digit*)?)
Digit <- [0-9]
True <- "#t"
False <- "#f"
`));

struct Atom { string name; }
struct List { Object[] data; }
struct DottedList
{
Object[] data;
Object tail;
}

alias Object = SumType!(
Atom,
List,
long,
string,
bool);

void main(string[] args)
{
enum input1 = `
(f0 #t #f '(1 2 3) '())
(f1 "string"
;; comment
(+ 20 -30))
`;
writeln(SchemeParser(input1));
// writeln("Hello, ", args[1]);
}

0 comments on commit f703122

Please sign in to comment.