Skip to content
This repository has been archived by the owner on Dec 5, 2019. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
Handle arguments in function calls
  • Loading branch information
Laura Savino committed Jun 12, 2012
1 parent a546079 commit 093134d
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 7 deletions.
15 changes: 11 additions & 4 deletions generator.hs
Expand Up @@ -22,6 +22,12 @@ putLabel s name = do
hPutStrLn (tmpHandle s) (name ++ ":")
return s

putArguments :: GeneratorState -> [Expression] -> IO GeneratorState
putArguments s [] = return s
putArguments s (expr : args) = do
finalState <- putExpression s expr
putArguments finalState args

putExpression :: GeneratorState -> Expression -> IO GeneratorState

putExpression s (NumberLiteral num) = do
Expand Down Expand Up @@ -49,7 +55,7 @@ putExpression s (FunctionCall name args) = do
tmpFD = tmpHandle s

let c = counter s
finalState = GeneratorState {
nextState = GeneratorState {
counter = c + 1,
tmpHandle = tmpFD,
outputHandle = outFD
Expand All @@ -60,10 +66,11 @@ putExpression s (FunctionCall name args) = do

hPutStrLn outFD ("@string" ++ (show c) ++ " = private unnamed_addr constant " ++ strType ++ " c\"" ++ (show name) ++ "\\00\"")

putStatement s ("%string" ++ (show c) ++ " = getelementptr inbounds " ++ strType ++ "* @string" ++ (show c) ++ ", i64 0, i64 0")
putStatement s ("call %getglobal_fp @getglobal (%lua_State* %state, i8* %string" ++ (show c) ++ ")")
putStatement s ("call %lua_call_fp @lua_call (%lua_State* %state, i32 0, i32 0)")
putStatement nextState ("%string" ++ (show c) ++ " = getelementptr inbounds " ++ strType ++ "* @string" ++ (show c) ++ ", i64 0, i64 0")
putStatement nextState ("call %getglobal_fp @getglobal (%lua_State* %state, i8* %string" ++ (show c) ++ ")")
finalState <- putArguments nextState args

putStatement finalState ("call %lua_call_fp @lua_call (%lua_State* %state, i32 " ++ (show $ length args) ++ ", i32 1)")
return finalState

-- Writes the file's header, the root of the AST, and the footer
Expand Down
2 changes: 1 addition & 1 deletion input.lua
@@ -1 +1 @@
dofile()
print(5, 4, 3, 2)
5 changes: 3 additions & 2 deletions parser.hs
Expand Up @@ -23,6 +23,7 @@ parens = Token.parens lexer
identifier = Token.identifier lexer
number = Token.naturalOrFloat lexer
whiteSpace = Token.whiteSpace lexer
commaSep = Token.commaSep lexer

-- Defines all Lua operators, their precedence, and their associativity
operators = [
Expand All @@ -44,8 +45,8 @@ prefixexp =
functioncall = do
var <- identifier
spaces
parens spaces
return $ FunctionCall (Name var) []
args <- parens (commaSep Parser.exp)
return $ FunctionCall (Name var) args

exp :: Parser Expression
exp = buildExpressionParser operators prefixexp

0 comments on commit 093134d

Please sign in to comment.