【发布时间】:2021-07-20 13:11:36
【问题描述】:
我收到以下错误,但我无法弄清楚为什么会这样。任何帮助表示赞赏。该程序是 Ocaml 中的解释器。 tryOcaml 下划线的那段代码是代码的最后一位“处理器令牌[];”不知道为什么会是类型不匹配或不匹配在代码中发生的位置。
错误:此表达式的类型为 stackValue list 但是需要一个单位类型的表达式
type stackValue = NUM of int | BOOL of bool | ERROR | STRING of string | NAME of string | UNIT
type com = PUSH of stackValue | POP | ADD | SUB | MUL | DIV | REM | NEG | SWAP | QUIT | TOSTRING | PRINTLN (*| CAT | AND | OR | NOT | LESSTHAN | EQUAL | IF | BIND let com {com} end | funBind com {com} [return] funEnd | call*)
let interpreter ( (inFile : string), (outFile : string )) : unit =
let ic = open_in inFile
in
let oc = open_out outFile
in
let rec loop_read acc =
try
let l = String.trim(input_line ic) in loop_read (l::acc)
with
| End_of_file -> List.rev acc
in
let ls_str = loop_read []
in
let checkrest s =
match s.[0] with
| '"' -> if s.[(String.length s) -1]='"' then STRING(String.sub s 1 ((String.length s)-2)) else ERROR
| '-' | '0'..'9' -> (try NUM(int_of_string s) with _ ->ERROR)
| '_' | 'a'..'z' | 'A'..'Z' -> NAME(s)
|_ -> ERROR
in
let str2sv s =
match s with
|":true:" -> BOOL(true)
|":false:" -> BOOL(false)
|":unit:" -> UNIT
|":error:" -> ERROR
|_-> checkrest s
in
let str2com s =
match s with
|"quit" -> QUIT
|"add" -> ADD
|"sub" -> SUB
|"mul" -> MUL
|"div" -> DIV
|"rem" -> REM
|"pop" -> POP
|"neg" -> NEG
|"swap" -> SWAP
(*|"cat" -> CAT
|"and" -> AND
|"or" -> OR
|"not" -> NOT
|"lessthan" -> LESSTHAN
|"equal" -> EQUAL
|"if" -> IF
|"bind" -> BIND*)
|"toString" -> TOSTRING
|_ -> if String.sub s 0 4 = "push" then let x = str2sv (String.sub s 5 ((String.length s) -5)) in PUSH(x) else PUSH(ERROR)
in
let tokens = List.map str2com ls_str
in
let sv2str sv =
match sv with
|BOOL(true) -> STRING(":true:")
|BOOL(false) -> STRING(":false:")
|UNIT -> STRING(":unit:")
|ERROR -> STRING(":error:")
|STRING(s) -> STRING(s)
|NAME(s) -> STRING(s)
|NUM(x) -> STRING(string_of_int(x))
in
let file_write value = Printf.fprintf oc "%s\n" value
in
let rec processor comlist stack =
match (comlist,stack) with
|(PUSH(x)::comst, stack) -> processor comst (x::stack)
|(POP::comst, stackValue::reststack) -> processor comst reststack
|(POP::comst, []) -> processor comst (ERROR::stack)
|(ADD::comst, NUM(a)::NUM(b)::reststack) -> processor comst (NUM(a+b)::reststack)
|(ADD::comst, stack) -> processor comst (ERROR::stack)
|(SUB::comst, NUM(a)::NUM(b)::reststack) -> processor comst (NUM(b-a)::reststack)
|(SUB::comst, stack) -> processor comst (ERROR::stack)
|(MUL::comst, NUM(a)::NUM(b)::reststack) -> processor comst (NUM(a*b)::reststack)
|(MUL::comst, stack) -> processor comst (ERROR::stack)
|(DIV::comst, NUM(a)::NUM(b)::reststack) -> if (NUM(a)=NUM(0)) then (processor comst (ERROR::NUM(a)::NUM(b)::reststack)) else (processor comst (NUM(b/a)::reststack))
|(DIV::comst, stack) -> processor comst (ERROR::stack)
|(REM::comst, NUM(a)::NUM(b)::reststack) -> if (NUM(a)=NUM(0)) then (processor comst (ERROR::NUM(a)::NUM(b)::reststack)) else (processor comst (NUM(b mod a)::reststack))
|(REM::comst, stack) -> processor comst (ERROR::stack)
|(NEG::comst, NUM(a)::reststack) -> processor comst (NUM(-a)::reststack)
|(NEG::comst, stack) -> processor comst (ERROR::stack)
|(SWAP::comst, x::xs::reststack) -> processor comst (xs::x::reststack)
|(SWAP::comst, stack) -> processor comst (ERROR::stack)
|(TOSTRING::comst, x::reststack) -> let s = sv2str x in processor comst (s::reststack)
|(TOSTRING::comst, []) -> processor comst (ERROR::stack)
|(PRINTLN::comst, x::reststack) -> (match x with
|STRING(x) -> file_write x; processor comst reststack
|_ -> ERROR::stack)
|(PRINTLN::comst, []) -> processor comst (ERROR::stack)
|(QUIT::comst, stack) -> []
|(_,_) -> []
in
processor tokens [];;
【问题讨论】:
-
具体有什么不明白的?您指定
interpreter函数应返回unit,但其中的最后一个表达式是对返回stackValue list的processor函数的调用。因此类型不匹配。 -
我猜如何返回一个单位?不知道如何返回一个单元,我真的不需要这个单元来做任何事情,因为这个程序的功能就像一个 void 函数并处理处理器内部的所有事情。我对 Ocaml 很陌生,这种功能不是我的强项。
标签: syntax compiler-errors functional-programming ocaml