【发布时间】:2020-07-02 04:00:20
【问题描述】:
我有一个使用此代码使用其他所有功能的主要功能:
File.read!(file_path)
|> Sanitizer.sanitize_source()
|> IO.inspect(label: "\nSanitizer ouput")
|> Lexer.scan_words()
|> IO.inspect(label: "\nLexer ouput")
|> Parser.parse_program()
|> IO.inspect(label: "\nParser ouput")
|> CodeGenerator.generate_code()
|> Linker.generate_binary(assembly_path
但是任何时候函数返回一个错误,其余的都会导致程序崩溃。我的老师告诉我改用with 来解决这个问题,在阅读了文档后我想出了这个:
with {:ok, contentF} <- File.read!(file_path)
sanitizedList when is_list(sanitizedList) <- Sanitizer.sanitize_source(contentF)
{:ok, _sMessage} <- IO.inspect(sanitizedList, label: "\nSanitizer output")
lexedList when not is_tuple(errorLex) <- Lexer.scan_words(sanitizedList)
{:ok, _sMessage} <- IO.inspect(lexedList, label: "\nLexer output")
parsedAST when not is_tuple(errorPar) <- Parser.parse_program(lexedList)
{:ok, _sMessage} <- IO.inspect(parsedAST, label: "\nParser output")
{:ok, codeAssembly} <- CodeGenerator.generate_code(parsedAST)
:ok <- Linker.generate_binary(codeAssembly, assembly_path)
do
{:ok, "compilation complete"}
else
error -> {:error, "error: couldn't compile the file" <> file_path}
end
根据我的阅读,这至少应该可以编译,但它给了我错误
(CompileError) lib/nqcc.ex:32: missing :do option in "with"
lib/nqcc.ex:28: (module)
我不确定为什么,我确实有一个do 声明。
如果有帮助,这就是每个函数都应该返回的内容:
Sanitizer.sanitize_source() 应该返回一个元组列表
Lexer.scan_words() 应该返回一个元组列表或{:error, "message"}
Parser.parse_program() 应该返回一个 AST 或 {:error, "message"}
【问题讨论】:
标签: elixir