Skip to content

Instantly share code, notes, and snippets.

@fcard
Last active August 29, 2015 14:27
Show Gist options
  • Select an option

  • Save fcard/a28fd02c2ba568626a09 to your computer and use it in GitHub Desktop.

Select an option

Save fcard/a28fd02c2ba568626a09 to your computer and use it in GitHub Desktop.
Julia Kaleidoscope Error
module ErrorTest
# VectorStream
type VectorStream{T}
vector :: Vector{T}
current :: Integer
VectorStream(vector) = new(vector, 0)
end
function next!(stream::VectorStream)
stream.current += 1
stream.vector[stream.current]
end
function peek!(stream::VectorStream)
stream.vector[stream.current+1]
end
# Tokens
module Tokens
abstract Token
immutable Keyword{K} <: Token end
immutable Numeric{T} <: Token value::T end
immutable Identifier <: Token name::Symbol end
end
# AST
module AST
abstract Expression
immutable Numeric{T<:Number} <: Expression
value::T
end
immutable Variable <: Expression
name::Symbol
end
immutable Call <: Expression
func::Symbol
args::Vector{Expression}
end
end
# Parser
function parse_expr!(tokens)
println("expr")
lhs=parse_primary!(tokens)
println("Should return: $lhs")
lhs
end
function parse_primary!(tokens)
println("primary 1")
parse_primary!(next!(tokens), tokens)
end
function parse_primary!(n::Tokens.Numeric, tokens)
println("primary 2")
AST.Numeric(n.value)
end
function parse_primary!(id::Tokens.Identifier, tokens)
println("primary 4")
parse_identifier!(id, peek!(tokens), tokens)
end
function parse_identifier!(id, p::Tokens.Keyword{'('}, tokens)
println("identifier 1")
next!(tokens)
call = AST.Call(id.name, AST.Expression[])
if peek!(tokens) != Tokens.Keyword{')'}()
parse_args!(tokens, call.args)
end
return call
end
function parse_identifier!(id, t, tokens)
println("identifier 2")
AST.Variable(id.name)
end
function parse_args!(tokens, args)
println("args")
ex = parse_expr!(tokens)
println("EX: ", ex)
println("TYPE: ", typeof(ex))
push!(args, ex)
continue_args(next!(tokens)) && parse_args!(tokens, args)
end
continue_args(t::Tokens.Keyword{','}) = true
continue_args(t::Tokens.Keyword{')'}) = false
# parse f(1)
const stream = VectorStream{Tokens.Token}([
Tokens.Identifier(:f),
Tokens.Keyword{'('}(),
Tokens.Numeric(1),
Tokens.Keyword{')'}()
])
println(parse_expr!(stream))
end
# julia 0.3
julia> include("c:/github/kaleidoscope/src/old/error_test.jl")
expr
primary 1
primary 4
identifier 1
args
expr
primary 1
primary 2
Should return: Numeric{Int64}(1)
EX: nothing
TYPE: nothing
ERROR: `convert` has no method matching convert(::Type{Expression}, ::Nothing)
in push! at array.jl:460
in parse_args! at c:\github\kaleidoscope\src\old\error_test.jl:98
in parse_identifier! at c:\github\kaleidoscope\src\old\error_test.jl:82
in parse_primary! at c:\github\kaleidoscope\src\old\error_test.jl:72
in parse_expr! at c:\github\kaleidoscope\src\old\error_test.jl:53
in include at boot.jl:245
in include_from_node1 at loading.jl:128
while loading c:\github\kaleidoscope\src\old\error_test.jl, in expression starti
ng on line 115
# julia 0.4
julia> include("c:/github/kaleidoscope/src/old/error_test.jl")
WARNING: replacing module ErrorTest
expr
primary 1
primary 4
identifier 1
args
expr
primary 1
primary 2
Should return: ErrorTest.AST.Numeric{Int64}(1)
EX: args
TYPE: args
ERROR: LoadError: MethodError: `convert` has no method matching convert(::Type{ErrorTest.AST.Expression}, ::ASCIIString)
This may have arisen from a call to the constructor ErrorTest.AST.Expression(...),
since type constructors fall back to convert methods.
Closest candidates are:
call{T}(::Type{T}, ::Any)
convert{T}(::Type{T}, ::T)
in push! at array.jl:419
in parse_args! at c:\github\kaleidoscope\src\old\error_test.jl:98
in parse_identifier! at c:\github\kaleidoscope\src\old\error_test.jl:82
in parse_primary! at c:\github\kaleidoscope\src\old\error_test.jl:72
in parse_expr! at c:\github\kaleidoscope\src\old\error_test.jl:53
in include at boot.jl:259
in include_from_node1 at loading.jl:266
while loading c:\github\kaleidoscope\src\old\error_test.jl, in expression starting on line 115
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment