編集: 問題は部分的に解決されました。更新のために一番下までスキップしてください。
私は Haskell を使用して小さな言語を書いており、多くの進歩を遂げましたが、「{ ... }」のようなブロックを使用するステートメントの実装に問題があります。パーサー ファイルで次のような If ステートメントのサポートを実装しました。
stmt = skip +++ ifstmt +++ assignment +++ whilestmt
ifstmt = symbol "if" >>
parens expr >>= \c ->
stmt >>= \t ->
symbol "else" >>
stmt >>= \e ->
return $ If c t e
whilestmt = symbol "while" >>
parens expr >>= \c ->
symbol "\n" >>
symbol "{" >>
stmt >>= \t ->
symbol "}" >>
return $ While c t
expr = composite +++ atomic
そして構文ファイルで:
class PP a where
pp :: Int -> a -> String
instance PP Stmt where
pp ind (If c t e) = indent ind ++
"if (" ++ show c ++ ") \n" ++
pp (ind + 2) t ++
indent ind ++ "else\n" ++
pp (ind + 2) e
pp ind (While c t) = indent ind ++
"while (" ++ show c ++") \n" ++
"{" ++ pp (ind + 2) t ++ "}" ++
indent ind
while ステートメントに何か問題があり、何がわかりません。ロジックは正しいようですが、コードを実行すると次のエラーが発生します。
EDIT: Fixed the first problem based on the first reply, now it is not recognizing my while statment which I assume comes from this:
exec :: Env -> Stmt -> Env
exec env (If c t e) =
exec env ( if eval env c == BoolLit True then t else e )
exec env (While c t) =
exec env ( if eval env c == BoolLit True then t )
読み取られるファイルは次のようになります。
x = 1; c = 0;
if (x < 2) c = c + 1; else ;
-- SEPARATE FILES FOR EACH
x = 1; c = 1;
while (x < 10)
{
c = c * x;
x = x + 1;
}
c
エラー レポートを理解しようとしましたが、何も問題を解決できませんでした。