Permalink
Browse files

add Postfix nodes, rename Unary to Prefix

  • Loading branch information...
1 parent 742a97a commit 4ccee9214b4576354cf935d50cb63ff38c166d93 @vito committed Dec 1, 2011
View
@@ -136,9 +136,13 @@ module(Atomy::AST):
Splice unamb := pretty
String unamb := pretty
ToplevelConstant unamb := pretty
- Unary unamb := pretty
+ Prefix unamb := pretty
+ Postfix unamb := pretty
Unquote unamb := pretty
- Word unamb := pretty
+ Word unamb := doc:
+ if(from?(#postfix))
+ then: parens(pretty)
+ else: pretty
Pattern unamb := @pattern unamb
Node unamb := doc: parens(pretty)
@@ -181,6 +185,14 @@ module(Atomy::AST):
Macro pretty-print := doc:
text("macro") <+> parens(@pattern pretty) <+> @body pretty
+ Postfix pretty-print := doc:
+ from(#postfix):
+ @receiver unamb <> text(@operator)
+
+ Prefix pretty-print := doc:
+ from(#prefix):
+ text(@operator) <> @receiver unamb
+
Primitive pretty-print := doc:
@value match:
#self -> text("self")
@@ -190,31 +202,27 @@ module(Atomy::AST):
x -> text(x inspect)
QuasiQuote pretty-print := doc:
- from(#unary):
+ from(#prefix):
text("`") <> @expression unamb
Quote pretty-print := doc:
- from(#unary):
+ from(#prefix):
text("'") <> @expression unamb
ScopedConstant pretty-print := doc:
from(#scoped-constant):
@parent unamb <> text("::") <> text(@name)
Splice pretty-print := doc:
- from(#unary):
+ from(#prefix):
text("~*") <> @expression unamb
String pretty-print := doc: text(@value inspect gsub("\\#", "#"))
ToplevelConstant pretty-print := doc: text("::") <> text(@name)
- Unary pretty-print := doc:
- from(#unary):
- text(@operator) <> @receiver unamb
-
Unquote pretty-print := doc:
- from(#unary):
+ from(#prefix):
text("~") <> @expression unamb
Word pretty-print := ident(@text to-s)
@@ -10,7 +10,7 @@ def arguments
case @pattern
when Binary
args = [@pattern.rhs]
- when Word, Unary
+ when Word, Prefix, Postfix
args = []
when Call
args = @pattern.arguments
@@ -36,7 +36,7 @@ def receiver
case @pattern
when Binary
recv = @pattern.lhs
- when Unary
+ when Prefix, Postfix
recv = @pattern.receiver
when Call, Word
recv = Primitive.new(@pattern.line, :self)
@@ -0,0 +1,23 @@
+module Atomy
+ module AST
+ class Postfix < Node
+ children :receiver
+ attributes :operator
+ generate
+
+ def bytecode(g)
+ pos(g)
+ @receiver.compile(g)
+ g.send message_name, 0
+ end
+
+ def message_name
+ :"#{@operator}@@"
+ end
+
+ def macro_name
+ :"atomy_macro::#{@operator}@@"
+ end
+ end
+ end
+end
@@ -1,6 +1,6 @@
module Atomy
module AST
- class Unary < Node
+ class Prefix < Node
children :receiver
attributes :operator
generate
View
@@ -100,7 +100,7 @@
delim(c) = (wsp "," wsp) | (sp "\n" sp)+ &{ current_column >= c }
- expression = level3
+ expression = level4
expressions = { current_column }:c expression:x (delim(c) expression)*:xs delim(c)?
{ [x] + Array(xs) }
@@ -118,17 +118,19 @@
| word
| block
| list
- | unary
+ | prefix
level1 = call
| grouped
| level0
- level2 = compose
+ level2 = postfix
| level1
- level3 = language
+ level3 = compose
+ | level2
+ level4 = language
| infix
| macro
| binary
- | level2
+ | level3
number = line:line < /[\+\-]?0[oO][0-7]+/ >
{ Atomy::AST::Primitive.new(line, text.to_i(8)) }
@@ -154,13 +156,13 @@
set_lang(n) = { @_grammar_lang = require("#{n}/language/parser").new(nil) }
language = ".language" wsp identifier:n set_lang(n) %lang.root
- quote = line:line "'" level1:e
+ quote = line:line "'" level2:e
{ Atomy::AST::Quote.new(line, e) }
- quasi_quote = line:line "`" level1:e
+ quasi_quote = line:line "`" level2:e
{ Atomy::AST::QuasiQuote.new(line, e) }
- splice = line:line "~*" level1:e
+ splice = line:line "~*" level2:e
{ Atomy::AST::Splice.new(line, e) }
- unquote = line:line "~" level1:e
+ unquote = line:line "~" level2:e
{ Atomy::AST::Unquote.new(line, e) }
escape = number_escapes | escapes
@@ -186,8 +188,11 @@
word = line:line identifier:n
{ Atomy::AST::Word.new(line, n) }
- unary = line:line !":" op_letter:o level1:e
- { Atomy::AST::Unary.new(line, e, o) }
+ prefix = line:line !":" op_letter:o level2:e
+ { Atomy::AST::Prefix.new(line, e, o) }
+
+ postfix = line:line level1:e operator:o {} # these brackets are significant (kpeg bug)
+ { Atomy::AST::Postfix.new(line, e, o) }
block = line:line ":" !operator wsp expressions?:es (wsp ";")?
{ Atomy::AST::Block.new(line, Array(es), []) }
@@ -197,9 +202,9 @@
list = line:line "[" wsp expressions?:es wsp "]"
{ Atomy::AST::List.new(line, Array(es)) }
- composes(pos) = line:line compose:l cont(pos) level1:r
+ composes(pos) = line:line compose:l cont(pos) level2:r
{ Atomy::AST::Compose.new(line, l, r) }
- | line:line level1:l cont(pos) level1:r
+ | line:line level2:l cont(pos) level2:r
{ Atomy::AST::Compose.new(line, l, r) }
compose = @composes(current_position)
@@ -208,9 +213,9 @@
call = line:line level0:n args:as
{ Atomy::AST::Call.new(line, n, as) }
- binary_c(pos) = (cont(pos) operator:o sig_wsp level2:e { [o, e] })+:bs
+ binary_c(pos) = (cont(pos) operator:o sig_wsp level3:e { [o, e] })+:bs
{ bs.flatten }
- binary = level2:l binary_c(current_position):c
+ binary = level3:l binary_c(current_position):c
{ resolve(nil, l, c).first }
| line:line operator:o sig_wsp expression:r
{ Atomy::AST::Binary.new(
Oops, something went wrong.

0 comments on commit 4ccee92

Please sign in to comment.