Mercurial > repos > rhope
diff parse.rhope @ 2:73e978d590c7
Adding WIP compiler code
author | Mike Pavone <pavone@retrodev.com> |
---|---|
date | Wed, 29 Apr 2009 02:58:03 -0400 |
parents | |
children | 0c5f24b4f69d |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/parse.rhope Wed Apr 29 02:58:03 2009 -0400 @@ -0,0 +1,326 @@ + +Blueprint Error +{ + Message + Line + Column +} + +Error[msg,text:out] +{ + out <- [[[Build["Error"]]Message <<[msg]]Line << [ [[text]Line >>]+[1] ]]Column << [ [[text]Column >>]+[1] ] +} + +To String@Error[error:out] +{ + out <- [[[[[error]Message >>]Append[" on line "]]Append[[error]Line >>]]Append[" at column "]]Append[[error]Column >>] +} + +Blueprint PImport Node +{ + File +} + +PImport Node[file:out] +{ + out <- [Build["PImport Node"]]File <<[file] +} + +Blueprint Worker Node +{ + Name + Blueprint + Inputs + Outputs + Tree +} + +Add Node Input@Worker Node[node,input:out] +{ + Print["Add Node Input"] + Print[["Input: "]Append[To String[input]]] + out <- [node]Inputs <<[[[node]Inputs >>]Append[input]] +} + +Add Node Output@Worker Node[node,output:out] +{ + out <- [node]Outputs <<[[[node]Outputs >>]Append[output]] +} + +Body[node,tokens,current,depth:out,out index, done] +{ + Print[["Body: Depth="]Append[depth]] + If[[depth] > [0]] + { + token <- [tokens]Index[current] + [token]Type Match["Block Begin"] + { + ,end stream <- [tokens]Next[current] + { + out,out index, done <- Body[node,tokens,~,[depth]+[1]] + } + }{ + [token]Type Match["Block End"] + { + [tokens]Next[current] + { + out,out index, done <- Body[node,tokens,~,[depth]-[1]] + }{ + ,end stream <- If[[depth] = [1]] + { + Print["done"] + out <- Val[node] + done <- Yes + } + } + }{ + ,end stream <- [tokens]Next[current] + { + out,out index, done <- Body[node,tokens,~,depth] + } + } + } + }{ + out <- node + out index <- current + } + Val[end stream] + { + Print[To String[Error[["Unexpected end of stream after token of type "]Append[[token]Type >>], [token]Raw Text >>]]] + } +} + +Before Body[node,tokens,current:out,out index, done] +{ + Print["Before body"] + token <- [tokens]Index[current] + next,end stream <- [tokens]Next[current] + { + [token]Type Match["Block Begin"] + { + out,out index, done <- Body[node,tokens,next,1] + }{ + out,out index, done <- Before Body[node,tokens,next] + } + } + Val[end stream] + { + Print[To String[Error[["Unexpected end of stream after token of type "]Append[[token]Type >>], [token]Raw Text >>]]] + } +} + +Outputs[node,tokens,current:out,out index, done] +{ + Print["outputs"] + token <- [tokens]Index[current] + next,end stream <- [tokens]Next[current] + { + [token]Type Match["Symbol"] + { + out,out index, done <- Outputs[[node]Add Node Output[[token]Text >>], tokens, next] + }{ + [token]Type Match["Args End"] + { + out,out index, done <- Before Body[node, tokens, next] + }{ + [token]Type Match[("List Separator","Block Comment","Line Comment","Newline")] + { + out,out index, done <- Outputs[node, tokens, next] + }{ + Print[To String[Error[["Unexpected token of type "]Append[[token]Type >>], [token]Raw Text >>]]] + } + } + } + } + Val[end stream] + { + Print[To String[Error[["Unexpected end of stream after token of type "]Append[[token]Type >>], [token]Raw Text >>]]] + } +} + +Inputs[node,tokens,current:out,out index, done] +{ + Print["Inputs"] + token <- [tokens]Index[current] + next,end stream <- [tokens]Next[current] + { + [token]Type Match["Symbol"] + { + + out,out index, done <- Inputs[[node]Add Node Input[[token]Text >>], tokens, next] + }{ + [token]Type Match["Name Separator"] + { + Print["in out sep"] + out,out index, done <- Outputs[node, tokens, next] + }{ + [token]Type Match["Args End"] + { + out,out index, done <- Before Body[node, tokens, next] + }{ + [token]Type Match[("List Separator","Block Comment","Line Comment","Newline")] + { + out,out index,done <- Inputs[node, tokens, next] + }{ + Print[To String[Error[["Unexpected token of type "]Append[[token]Type >>], [token]Raw Text >>]]] + } + } + } + } + } + Val[end stream] + { + Print[To String[Error[["Unexpected end of stream after token of type "]Append[[token]Type >>], [token]Raw Text >>]]] + } +} + +Method[node,tokens,current:out,out index,done] +{ + token <- [tokens]Index[current] + next <- [tokens]Next[current] + { + [token]Type Match["Symbol"] + { + out,out index,done <- Before Inputs[[node]Blueprint <<[[token]Text >>], tokens, next] + }{ + [token]Type Match[("List Separator","Block Comment","Line Comment","Newline")] + { + out,out index,done <- Method[node, tokens, next] + }{ + Print[To String[Error[["Unexpected token of type "]Append[[token]Type >>], [token]Raw Text >>]]] + } + } + }{ + Print[To String[Error[["Unexpected end of stream after token of type "]Append[[token]Type >>], [token]Raw Text >>]]] + } +} + +//TODO: support method declarations +Before Inputs[node, tokens, current:out, out index, done] +{ + Print["Before Inputs"] + token <- [tokens]Index[current] + [token]Type Match["Args Begin"] + { + [tokens]Next[current] + { + out,out index, done <- Inputs[node, tokens, ~] + }{ + Print[To String[Error[["Unexpected end of stream after token of type "]Append[[token]Type >>], [token]Raw Text >>]]] + } + }{ + [token]Type Match["Method Separator"] + { + [tokens]Next[current] + { + out,out index,done <- Method[node, tokens, ~] + }{ + Print[To String[Error[["Unexpected end of stream after token of type "]Append[[token]Type >>], [token]Raw Text >>]]] + } + }{ + [token]Type Match[("Line Comment","Block Comment","Newline")] + { + continue <- Yes + }{ + Print[To String[Error[["Unexpected token of type "]Append[[token]Type >>], [token]Raw Text >>]]] + } + Val[continue] + { + [tokens]Next[current] + { + out,out index, done <- Before Inputs[node, tokens, ~] + }{ + Print[To String[Error[["Unexpected end of stream after token of type "]Append[[token]Type >>], [token]Raw Text >>]]] + } + } + } + } +} + +Worker Node[name, tokens, current:out,out index, done] +{ + Print[["Worker: "]Append[To String[name]]] + out,out index, done <- Before Inputs[[[[[Build["Worker Node"]]Name <<[name]]Inputs <<[()]]Outputs <<[()]]Tree <<[()], tokens, current] +} + +Blueprint PBlueprint Node +{ + Name + Fields +} + +PBlueprint Node[name, tokens, current:out,out index,done] +{ + out <- [[Build["PBlueprint Node"]]Name <<[name]]Fields <<[()] + out index <- current + //TODO: Parse rest of blueprint definition +} + + +Top Level[tokens, current, nodes:out] +{ + token <- [tokens]Index[current] + Print[[token]Type >>] + [token]Type Match["Symbol"] + { + [[token]Text >>]After["Import "] + { + next nodes <- [nodes]Append[PImport Node[~]] + next index <- Val[current] + }{ + blueprint name,worker name <- [~]After["Blueprint "] + { + [tokens]Next[current] + { + , next index <- PBlueprint Node[blueprint name, tokens, ~] + { + next nodes <- [nodes]Append[~] + } {} { + Print["done!"] + out <- Val[next nodes] + } + }{ + Print[To String[Error[["Unexpected end of stream after symbol "]Append[[token]Text >>], [token]Raw Text >>]]] + } + }{ + [tokens]Next[current] + { + , next index <- Worker Node[worker name, tokens, ~] + { + next nodes <- [nodes]Append[~] + } {} { + Print["done!"] + out <- Val[next nodes] + } + }{ + Print[To String[Error[["Unexpected end of stream after symbol "]Append[[token]Text >>], [token]Raw Text >>]]] + } + } + } + + }{ + [token]Type Match[("Newline","Block Comment","Line Comment")] + { + next nodes <- Val[nodes] + next index <- Val[current] + }{ + Print[To String[Error[["Unexpected token of type "]Append[[token]Type >>], [token]Raw Text >>]]] + } + } + [tokens]Next[next index] + { + out <- Top Level[tokens, ~, next nodes] + }{ + out <- Val[next nodes] + } +} + +Parse[tokens:out] +{ + [tokens]First + { + out <- Top Level[tokens, ~, ()] + }{ + out <- () + } +}