Skip to content

Commit

Permalink
AST to SExpression...sexah!
Browse files Browse the repository at this point in the history
  • Loading branch information
unknown committed Jul 30, 2008
1 parent 078a4cc commit 895fd4d
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 13 deletions.
25 changes: 12 additions & 13 deletions support/parser.lua
Expand Up @@ -77,7 +77,7 @@ end

function parse( inCodeString )
local theASTRoot = parseToAST( inCodeString )
return codeFromAST( theASTRoot.program )
return codeFromAST( theASTRoot )
end

function parseToAST( inCodeString )
Expand All @@ -94,20 +94,19 @@ end

function codeFromAST( inASTNode )
local theResult
if inASTNode.type == "program" then
theResult = runtime.createList( )
for i,theChildNode in ipairs( inASTNode ) do
runtime.appendListItem( theResult, codeFromAST( theChildNode ) )
if inASTNode.type == "program" or inASTNode.type == "expression" then
local theHead
for i=#inASTNode,1,-1 do
theHead = runtime.join( codeFromAST( inASTNode[ i ] ), theHead )
end
theHead.quotedFlag = inASTNode.quotedFlag
theResult = theHead

elseif inASTNode.type == "expression" then
theResult = runtime.createList{ quotedFlag = inASTNode.quotedFlag }
for i,theChildNode in ipairs( inASTNode ) do
runtime.appendListItem( theResult, codeFromAST( theChildNode ) )
end

else -- symbol, number, string
theResult = runtime[ inASTNode.type ][ inASTNode.value ]
elseif inASTNode.type == "symbol" then
theResult = runtime.createValue( inASTNode.type, inASTNode.value, inASTNode.quotedFlag )

else -- number, string
theResult = inASTNode.value

end

Expand Down
15 changes: 15 additions & 0 deletions support/runtime.lua
@@ -0,0 +1,15 @@
module( 'runtime', package.seeall )

function join( inHeadValue, inTailValue )
return { head=inHeadValue, tail=inTailValue }
end

function createValue( inType, inValue, inQuotedFlag )
local theResult
if inType == 'symbol' then
theResult = { symbol=inValue, quotedFlag=inQuotedFlag }
else
error( "Unrecognized value type '"..inType.."'" )
end
return theResult
end
81 changes: 81 additions & 0 deletions test/test_parser.lua
Expand Up @@ -46,4 +46,85 @@ function test02_ast_results( )
)
end

function test03_codeFromAST( )
local theCode = "[foo bar]"
local theProgram = parser.parse( theCode )
assertTableEquals( theProgram, {
head = {
head = {
symbol = "foo"
},
tail = {
head = {
symbol = "bar"
},
}
},
} )

local theCode = "[foo 'bar 17 \"whee\"]"
local theProgram = parser.parse( theCode )
assertTableEquals( theProgram, {
head = {
head = {
symbol = "foo"
},
tail = {
head = {
symbol = "bar",
quotedFlag = true
},
tail = {
head = 17,
tail = {
head = "whee"
}
}
}
},
} )

local theCode = "[foo [bar]] [jim]"
local theProgram = parser.parse( theCode )
assertTableEquals( theProgram, {
head = {
head = {
symbol = "foo"
},
tail = {
head = {
head = {
symbol = "bar"
}
}
}
},
tail = {
head = {
head = {
symbol = "jim"
}
}
}
} )

local theCode = [===[
[define map inFunc inList
[pair [inFunc [head inList]]
[map 'inFunc [rest inList]]
]
]
[define square x
[* x x]
]
[print [map square '[1 2 3 4 5]]]
]===]

local theProgram = parser.parse( theCode )
-- assertTableEquals( theProgram, {
-- head={}
-- rest={}
-- })
end

runTests{ useHTML = true }

0 comments on commit 895fd4d

Please sign in to comment.