我有以下模塊:陣列型
module lexer
export parseCode
function parseCode(s::String)
lexeme::Array{UInt32, 1}
words = Dict("dup"=>UInt32(0x40000001), "drop"=>UInt32(0x40000002), "swap"=> UInt32(0x40000003), "+"=> UInt32(0x40000004), "-"=> UInt32(0x40000005), "x"=> UInt(0x4000000c),"/"=>UInt32(0x40000006), "%"=> UInt32(0x40000007), "if"=> UInt32(0x40000008),
"j"=> UInt32(0x40000009), "print"=> UInt32(0x4000000a), "exit"=>UInt32(b))
opcode = split(s)
for w in opcode
instruction::UInt32
instruction = get(words,w,0)
if instruction != 0
push!(lexeme,instruction)
end
end
push!(lexeme,UInt32(11))
return lexeme
end
end
功能parseCode解析中的字符串的話,併爲每個單詞取出對應的整數值,並將其推入一個數組詞法。 該函數然後返回數組test.jl:
require("stackProcessor")
require("lexer")
using stackProcessor
using lexer
#=prog=Array{UInt32,4}
prog=[3,4,0x40000001, 5, 0x40000002, 3,0x40000003, 2, 0x40000004, 0x40000000]
processor(prog)
=#
f = open("opcode.txt")
s = readall(f)
close(f)
print(s)
opcode = parseCode(s)
print(repr(opcode))
processor(opcode)
操作碼是應該得到的語義數組的副本的變量,但我得到以下錯誤:
oadError: UndefVarError: lexeme not defined
in parseCode at C:\Users\Administrator\AppData\Local\atom\app-1.10.2\julia\lexer.jl:11
in include_string at loading.jl:282
in include_string at C:\Users\Administrator\.julia\v0.4\CodeTools\src\eval.jl:32
in anonymous at C:\Users\Administrator\.julia\v0.4\Atom\src\eval.jl:84
in withpath at C:\Users\Administrator\.julia\v0.4\Requires\src\require.jl:37
in withpath at C:\Users\Administrator\.julia\v0.4\Atom\src\eval.jl:53
[inlined code] from C:\Users\Administrator\.julia\v0.4\Atom\src\eval.jl:83
in anonymous at task.jl:58
while loading C:\Users\Administrator\AppData\Local\atom\app-1.10.2\julia\test.jl, in expression starting on line 17
有趣的是它工作正常,現在它給了我這個錯誤。 我以爲在朱莉婭,數組返回作爲副本,所以我不能確定錯誤來自哪裏。
也許'語義「最初是從全球環境中遺留下來的,因此之前就起作用了。要麼全局地重新定義它,要麼'parseCode'函數需要改變。 –
@Dan:那麼我需要傳遞一個空數組作爲函數參數,然後用新內容填充數組? – JJTO