未定义的模块中的函数返回的数组类型

时间:2016-09-17 22:40:17

标签: 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), "-"=> 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中的单词并为每个单词提取相应的整数值,并将它们推送到数组lexeme中。 然后该函数将数组返回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中,数组作为副本返回,所以我无法确定错误的来源。

1 个答案:

答案 0 :(得分:3)

该行

lexeme::Array{UInt32, 1} 

听起来你希望初始化一个局部变量......但这不是那样做的。这只是现有变量的类型断言。我假设第11行产生了错误,对吗?

错误告诉您,此时您试图在第11行断言lexeme变量的类型,该特定变量尚未在函数中定义到该点。

据推测,在您清理工作区之前它已经有效了,因为它是作为全局变量存在的......

如果您想要初始化,请执行以下操作:

lexeme = Array{UInt32,1}(0);