aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authors-ol <s-ol@users.noreply.github.com>2020-02-01 19:30:19 +0000
committers-ol <s-ol@users.noreply.github.com>2020-02-01 19:36:56 +0000
commit9b05b893ab5c286c7b5e3c6185d2f103a4c08327 (patch)
tree187ec8beb556cf255524154baef9f431d5c4ca5a
parentbasic references (diff)
downloadalive-9b05b893ab5c286c7b5e3c6185d2f103a4c08327.tar.gz
alive-9b05b893ab5c286c7b5e3c6185d2f103a4c08327.zip
ast testing + fix
-rw-r--r--ast.moon13
-rw-r--r--spec/ast_spec.moon64
2 files changed, 63 insertions, 14 deletions
diff --git a/ast.moon b/ast.moon
index 47eebee..c5bfd07 100644
--- a/ast.moon
+++ b/ast.moon
@@ -1,12 +1,6 @@
import Const from require 'base'
unpack or= table.unpack
-unescape = (str) ->
- str = str\gsub '\\"', '"'
- str = str\gsub "\\'", "'"
- str = str\gsub "\\\\", "\\"
- str
-
class ASTNode
-- first pass (outin):
-- * expand macros
@@ -23,13 +17,18 @@ class Atom
new: (@raw, @atom_type) =>
+ unescape = (str) ->
+ str = str\gsub '\\"', '"'
+ str = str\gsub "\\'", "'"
+ str = str\gsub "\\\\", "\\"
+ str
expand: (scope) =>
@value = Const switch @atom_type
when 'num'
tonumber @raw
when 'sym'
assert scope[@raw], "undefined reference to symbol '#{@raw}'"
- when 'strq', 'std'
+ when 'strq', 'strd'
unescape @raw
else
error "unknown atom type: '#{@atom_type}'"
diff --git a/spec/ast_spec.moon b/spec/ast_spec.moon
index 00f0c29..6e5ca32 100644
--- a/spec/ast_spec.moon
+++ b/spec/ast_spec.moon
@@ -1,16 +1,66 @@
import Atom, Xpr from require 'ast'
-describe 'AST', ->
+describe 'Atom', ->
+ expand = (typ, str, ...) ->
+ atom = Atom["make_#{typ}"] str
+ atom\expand ...
+ atom.value\getc!
+
+ describe 'sym', ->
+ it 'expand correctly', ->
+ env = { a: 1, b: 2, c: 44, 'long_name': 'str',
+ 'name/with/slash': {} }
+
+ for k,v in pairs env
+ assert.is.equal v, expand 'sym', k, env
+
+ describe 'num', ->
+ it 'expand correctly', ->
+ assert.is.equal 1, expand 'num', '1'
+ assert.is.equal 0, expand 'num', '0'
+ assert.is.equal .1, expand 'num', '.1'
+ assert.is.equal .123, expand 'num', '.123'
+ assert.is.equal 20, expand 'num', '20'
+ assert.is.equal 20, expand 'num', '20.'
+ assert.is.equal 20.1, expand 'num', '20.1'
+
+ describe 'strd', ->
+ it 'expand correctly', ->
+ assert.is.equal 'hello', expand 'strd', 'hello'
+ assert.is.equal 'hello world', expand 'strd', 'hello world'
+ assert.is.equal '', expand 'strd', ''
+ assert.is.equal '\\', expand 'strd', '\\\\'
+ assert.is.equal "'", expand 'strd', "\\'"
+ assert.is.equal '"', expand 'strd', '\\"'
+ assert.is.equal "a string with ' inside",
+ expand 'strd', "a string with ' inside"
+
+ describe 'strq', ->
+ it 'expand correctly', ->
+ assert.is.equal 'hello', expand 'strq', 'hello'
+ assert.is.equal 'hello world', expand 'strq', 'hello world'
+ assert.is.equal '', expand 'strq', ''
+ assert.is.equal '\\', expand 'strq', '\\\\'
+ assert.is.equal "'", expand 'strq', "\\'"
+ assert.is.equal '"', expand 'strq', '\\"'
+ assert.is.equal 'a string with " inside',
+ expand 'strq', 'a string with " inside'
+
+describe 'Xpr', ->
+ describe 'can be tagged', ->
+ xpr = Xpr.make_sexpr 2, {''}
+ assert.is.equal 2, xpr.tag
+ assert.is.equal '([2])', xpr\stringify!
+
describe 'can be walked', ->
a1 = Atom.make_num '1'
a2 = Atom.make_num '2'
a3 = Atom.make_num '3'
- x1 = Xpr '(', { '', a1, '' }
- x21 = Xpr '(', { '', a2, '' }
- x22 = Xpr '(', { '', a3, '' }
- x2 = Xpr '(', { '', x21, ' ', x22, '' }
-
- root = Xpr 'naked', { '', x1, ' ', x2, '' }
+ x1 = Xpr.make_sexpr { '', a1, '' }
+ x21 = Xpr.make_sexpr { '', a2, '' }
+ x22 = Xpr.make_sexpr { '', a3, '' }
+ x2 = Xpr.make_sexpr { '', x21, ' ', x22, '' }
+ root = Xpr.make_nexpr { '', x1, ' ', x2, '' }
assert_yields = (expected_order, iter) ->
for val in *expected_order