2022-02-09 00:26:58 +00:00
|
|
|
|
module Luau.Syntax.ToString where
|
|
|
|
|
|
2022-03-02 22:02:51 +00:00
|
|
|
|
open import Agda.Builtin.Bool using (true; false)
|
2022-02-18 19:09:00 +00:00
|
|
|
|
open import Agda.Builtin.Float using (primShowFloat)
|
2022-03-02 22:02:51 +00:00
|
|
|
|
open import Luau.Syntax using (Value; Block; Stat; Expr; VarDec; FunDec; nil; bool; val; var; var_∈_; addr; _$_; function_is_end; return; local_←_; _∙_; done; block_is_end; _⟨_⟩; _⟨_⟩∈_; number; BinaryOperator; +; -; *; /; <; >; ==; ~=; <=; >=; binexp)
|
2022-02-09 00:26:58 +00:00
|
|
|
|
open import FFI.Data.String using (String; _++_)
|
2022-02-09 23:14:29 +00:00
|
|
|
|
open import Luau.Addr.ToString using (addrToString)
|
2022-02-12 01:03:26 +00:00
|
|
|
|
open import Luau.Type.ToString using (typeToString)
|
2022-02-09 23:14:29 +00:00
|
|
|
|
open import Luau.Var.ToString using (varToString)
|
2022-02-09 00:26:58 +00:00
|
|
|
|
|
2022-02-12 01:03:26 +00:00
|
|
|
|
varDecToString : ∀ {a} → VarDec a → String
|
|
|
|
|
varDecToString (var x) = varToString x
|
|
|
|
|
varDecToString (var x ∈ T) = varToString x ++ " : " ++ typeToString T
|
|
|
|
|
|
|
|
|
|
funDecToString : ∀ {a} → FunDec a → String
|
|
|
|
|
funDecToString ("" ⟨ x ⟩∈ T) = "function(" ++ varDecToString x ++ "): " ++ typeToString T
|
|
|
|
|
funDecToString ("" ⟨ x ⟩) = "function(" ++ varDecToString x ++ ")"
|
|
|
|
|
funDecToString (f ⟨ x ⟩∈ T) = "function " ++ varToString f ++ "(" ++ varDecToString x ++ "): " ++ typeToString T
|
|
|
|
|
funDecToString (f ⟨ x ⟩) = "function " ++ varToString f ++ "(" ++ varDecToString x ++ ")"
|
|
|
|
|
|
2022-02-22 23:52:56 +00:00
|
|
|
|
binOpToString : BinaryOperator → String
|
|
|
|
|
binOpToString + = "+"
|
|
|
|
|
binOpToString - = "-"
|
|
|
|
|
binOpToString * = "*"
|
|
|
|
|
binOpToString / = "/"
|
2022-02-24 19:17:46 +00:00
|
|
|
|
binOpToString < = "<"
|
|
|
|
|
binOpToString > = ">"
|
2022-03-02 22:02:51 +00:00
|
|
|
|
binOpToString == = "=="
|
|
|
|
|
binOpToString ~= = "~="
|
|
|
|
|
binOpToString <= = "<="
|
|
|
|
|
binOpToString >= = ">="
|
|
|
|
|
|
|
|
|
|
valueToString : Value → String
|
|
|
|
|
valueToString nil = "nil"
|
|
|
|
|
valueToString (addr a) = addrToString a
|
|
|
|
|
valueToString (number x) = primShowFloat x
|
|
|
|
|
valueToString (bool false) = "false"
|
|
|
|
|
valueToString (bool true) = "true"
|
2022-02-22 23:52:56 +00:00
|
|
|
|
|
2022-02-12 01:03:26 +00:00
|
|
|
|
exprToString′ : ∀ {a} → String → Expr a → String
|
|
|
|
|
statToString′ : ∀ {a} → String → Stat a → String
|
|
|
|
|
blockToString′ : ∀ {a} → String → Block a → String
|
2022-02-09 00:26:58 +00:00
|
|
|
|
|
2022-03-02 22:02:51 +00:00
|
|
|
|
exprToString′ lb (val v) =
|
|
|
|
|
valueToString(v)
|
2022-02-09 00:26:58 +00:00
|
|
|
|
exprToString′ lb (var x) =
|
2022-02-09 23:14:29 +00:00
|
|
|
|
varToString(x)
|
2022-02-09 00:26:58 +00:00
|
|
|
|
exprToString′ lb (M $ N) =
|
|
|
|
|
(exprToString′ lb M) ++ "(" ++ (exprToString′ lb N) ++ ")"
|
2022-02-12 01:03:26 +00:00
|
|
|
|
exprToString′ lb (function F is B end) =
|
|
|
|
|
funDecToString F ++ lb ++
|
2022-02-09 00:26:58 +00:00
|
|
|
|
" " ++ (blockToString′ (lb ++ " ") B) ++ lb ++
|
|
|
|
|
"end"
|
2022-02-09 23:14:29 +00:00
|
|
|
|
exprToString′ lb (block b is B end) =
|
2022-03-02 22:02:51 +00:00
|
|
|
|
"(" ++ varDecToString b ++ "()" ++ lb ++
|
2022-02-09 23:14:29 +00:00
|
|
|
|
" " ++ (blockToString′ (lb ++ " ") B) ++ lb ++
|
|
|
|
|
"end)()"
|
2022-02-22 23:52:56 +00:00
|
|
|
|
exprToString′ lb (binexp x op y) = exprToString′ lb x ++ " " ++ binOpToString op ++ " " ++ exprToString′ lb y
|
2022-02-09 00:26:58 +00:00
|
|
|
|
|
2022-02-12 01:03:26 +00:00
|
|
|
|
statToString′ lb (function F is B end) =
|
|
|
|
|
"local " ++ funDecToString F ++ lb ++
|
2022-02-09 00:26:58 +00:00
|
|
|
|
" " ++ (blockToString′ (lb ++ " ") B) ++ lb ++
|
|
|
|
|
"end"
|
|
|
|
|
statToString′ lb (local x ← M) =
|
2022-02-12 01:03:26 +00:00
|
|
|
|
"local " ++ varDecToString x ++ " = " ++ (exprToString′ lb M)
|
2022-02-09 00:26:58 +00:00
|
|
|
|
statToString′ lb (return M) =
|
|
|
|
|
"return " ++ (exprToString′ lb M)
|
|
|
|
|
|
2022-02-09 23:14:29 +00:00
|
|
|
|
blockToString′ lb (S ∙ done) = statToString′ lb S
|
2022-02-09 00:26:58 +00:00
|
|
|
|
blockToString′ lb (S ∙ B) = statToString′ lb S ++ lb ++ blockToString′ lb B
|
2022-02-09 23:14:29 +00:00
|
|
|
|
blockToString′ lb (done) = ""
|
2022-02-09 00:26:58 +00:00
|
|
|
|
|
2022-02-12 01:03:26 +00:00
|
|
|
|
exprToString : ∀ {a} → Expr a → String
|
2022-02-09 00:26:58 +00:00
|
|
|
|
exprToString = exprToString′ "\n"
|
|
|
|
|
|
2022-02-12 01:03:26 +00:00
|
|
|
|
statToString : ∀ {a} → Stat a → String
|
2022-02-09 00:26:58 +00:00
|
|
|
|
statToString = statToString′ "\n"
|
|
|
|
|
|
2022-02-12 01:03:26 +00:00
|
|
|
|
blockToString : ∀ {a} → Block a → String
|
2022-02-09 00:26:58 +00:00
|
|
|
|
blockToString = blockToString′ "\n"
|