Arrays 未定义模块中的函数返回的数组类型
我有以下模块:Arrays 未定义模块中的函数返回的数组类型,arrays,module,julia,Arrays,Module,Julia,我有以下模块: 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), "-"=> UI
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解析字符串s中的单词,并为每个单词获取相应的整数值,然后将它们推送到数组词素中。
然后,该函数将数组返回到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)
Opcode是应该获取lexeme数组副本的变量,但我得到以下错误:
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
有趣的是,它工作得很好,现在它给了我这个错误。
我认为在Julia中,数组是作为副本返回的,因此我无法确定错误来自何处。行
lexeme::Array{UInt32, 1}
听起来你想初始化一个局部变量。。。但那不是它的作用。这只是现有变量的类型断言。我假设是第11行产生了错误,对吗
错误告诉您,在您试图在第11行断言lexeme变量的类型时,函数中的特定变量尚未定义到该点
大概它在你清理你的工作区之前起作用了,因为它是作为一个全局变量或其他东西出现的
如果要初始化,请执行以下操作:
lexeme = Array{UInt32,1}(0);
也许
lexeme
最初是从全球环境中遗留下来的,因此它以前就可以工作了。要么全局重新定义它,要么函数parseCode
需要更改。@丹:那么我需要传递一个空数组作为函数参数,然后用新内容填充数组吗?我忽略了(0)。我搞砸了…谢谢。更简单的语法是UInt32[]
。