loading
22 |$locstr
" 17 | if !(node isa Nothing) 18 | str *= "Inferred:
$(refff(node[1]))
" 19 | end 20 | return str 21 | end 22 | let 23 | ctx = I.GlobalContext() 24 | mi = which(sin, (Float64,)).specializations[1] 25 | mod = Base 26 | e = I.Engine(ctx, mi, mod) 27 | c = I.Context() 28 | str = """ 29 | begin 30 | x = 1 31 | y = 2 32 | z = (x, y) 33 | x += 1 34 | l = 'a' 35 | k = :x 36 | o = quote a end 37 | p = Vector{Int}(undef, 1) 38 | sort(p;rev=true) 39 | w = Vector{Union{Int, Nothing}}()[1] 40 | if w isa Int 41 | print(w) 42 | k = 1 43 | else 44 | error() 45 | end 46 | p[1] += 1 47 | p[1] += "x" 48 | (x1, (x2, x3)) = (3,(4,'a')) 49 | x1 = 'a' 50 | x1 = 1.0 51 | end 52 | """ 53 | ast = SimpleTypeChecker.SyntaxAdaptor.parseAndConstructHTML(str, "none", joinpath(@__DIR__, "../src/www/")).args[1] 54 | try 55 | r = I.inferExpr(e, c, ast) 56 | I.displayContext(stdout, r.ctx) 57 | f = SimpleTypeChecker.Server.QueryFunctor(ff, ast, e.flowMapping) 58 | SimpleTypeChecker.Server.handleQuery(f) 59 | catch e 60 | if e isa I.InferenceError 61 | println("────────────────────────────────────────────────────────────────") 62 | print(String(take!(ctx.errio.io))) 63 | println("────────────────────────────────────────────────────────────────") 64 | elseif e isa I.SyntaxError 65 | println(e.msg) 66 | else 67 | rethrow(e) 68 | end 69 | end 70 | 71 | end 72 | end 73 | ) 74 | 75 | =# 76 | 77 | #= 78 | Core.eval(MyTest.Wrapper, 79 | quote 80 | include("test.jl") 81 | ctx = SimpleTypeChecker.Inference.GlobalContext() 82 | SimpleTypeChecker.Inference.addFile!(ctx, TTest, abspath("test/test.jl")) 83 | SimpleTypeChecker.Inference.runCheck!(ctx) 84 | for (k,v) in ctx.hasChecked 85 | if v isa SimpleTypeChecker.Inference.InferenceError 86 | z = split(string(typeof(v)), '.')[end] 87 | x = z[length("InferenceError")+1:end] 88 | x = "displayError" * x 89 | f = getproperty(SimpleTypeChecker.Inference, Symbol(x)) 90 | f(v) 91 | end 92 | end 93 | println(String(take!(ctx.errio.io))) 94 | end 95 | ) 96 | =# 97 | #= 98 | Core.eval(MyTest.Wrapper, 99 | quote 100 | include("test.jl") 101 | ctx = SimpleTypeChecker.Inference.GlobalContext() 102 | SimpleTypeChecker.Inference.addFile!(ctx, TTest, abspath("test/test.jl")) 103 | for (meth, def) in ctx.methodDefs 104 | local sictx 105 | try 106 | sictx = SimpleTypeChecker.Inference.analyzeScopeVariable(ctx, def) 107 | catch e 108 | println(String(take!(ctx.errio.io))) 109 | rethrow(e) 110 | end 111 | println("For method : ", meth) 112 | for (_, info) in sort(collect(sictx.infos);by = x->x.second.parent.loc.span) 113 | println(info) 114 | end 115 | println("────────────────────────────────────────────────────────────────") 116 | end 117 | 118 | SimpleTypeChecker.Inference.runCheck!(ctx) 119 | end 120 | ) 121 | =# 122 | module MyTest 123 | include("../src/SimpleTypeChecker.jl") 124 | end 125 | Core.eval(MyTest, 126 | quote 127 | ctx = SimpleTypeChecker.Inference.GlobalContext() 128 | const path = abspath(joinpath(@__DIR__, "..")) 129 | SimpleTypeChecker.Inference.addFile!(ctx, SimpleTypeChecker.SyntaxAdaptor, joinpath(path, "src/adaptor/JSAdaptor.jl")) 130 | SimpleTypeChecker.Inference.addFile!(ctx, SimpleTypeChecker.SyntaxAdaptor, joinpath(path, "src/adaptor/TreeQuery.jl")) 131 | SimpleTypeChecker.Inference.addFile!(ctx, SimpleTypeChecker.Inference, joinpath(path, "src/adaptor/InferenceDefinition.jl")) 132 | SimpleTypeChecker.Inference.addFile!(ctx, SimpleTypeChecker.Inference, joinpath(path, "src/adaptor/Inference.jl")) 133 | SimpleTypeChecker.Inference.addFile!(ctx, SimpleTypeChecker.Inference, joinpath(path, "src/adaptor/InferenceErrorUtility.jl")) 134 | SimpleTypeChecker.Inference.addFile!(ctx, SimpleTypeChecker.Inference, joinpath(path, "src/adaptor/InferenceError.jl")) 135 | SimpleTypeChecker.Inference.addFile!(ctx, SimpleTypeChecker.Inference, joinpath(path, "src/adaptor/ScopeChecking.jl")) 136 | SimpleTypeChecker.Inference.addFile!(ctx, SimpleTypeChecker.Server, joinpath(path, "src/server/SimpleHTTPServer.jl")) 137 | SimpleTypeChecker.Inference.runCheck!(ctx) 138 | #= 139 | for (meth, def) in ctx.methodDefs 140 | local sictx 141 | try 142 | sictx = SimpleTypeChecker.Inference.analyzeScopeVariable(ctx, def) 143 | catch e 144 | println(String(take!(ctx.errio.io))) 145 | rethrow(e) 146 | end 147 | println("For method : ", meth) 148 | for (_, info) in sort(collect(sictx.infos);by = x->x.second.parent.loc.span) 149 | println(info) 150 | end 151 | println("────────────────────────────────────────────────────────────────") 152 | end 153 | 154 | SimpleTypeChecker.Inference.runCheck!(ctx) 155 | =# 156 | end 157 | ) 158 | 159 | 160 | Core.eval(MyTest, quote 161 | ctx = SimpleTypeChecker.Inference.GlobalContext() 162 | const path = abspath(joinpath(@__DIR__, "..")) 163 | include(joinpath(path, "test/case/scope.jl")) 164 | SimpleTypeChecker.Inference.addFile!(ctx, ScopeTest, joinpath(path, "test/case/scope.jl")) 165 | SimpleTypeChecker.Inference.runCheck!(ctx) 166 | end) -------------------------------------------------------------------------------- /test/refactor.jl: -------------------------------------------------------------------------------- 1 | module MyTest 2 | 3 | import SimpleTypeChecker 4 | 5 | module Wrapper 6 | include("../src/SimpleTypeChecker.jl") 7 | end 8 | 9 | const path = abspath(".") 10 | ctx = SimpleTypeChecker.Inference.GlobalContext() 11 | SimpleTypeChecker.API.addFile!(ctx, Wrapper.SimpleTypeChecker.SyntaxDefinition, joinpath(path, "src/adaptor/SyntaxDefinition.jl")) 12 | SimpleTypeChecker.API.addFile!(ctx, Wrapper.SimpleTypeChecker.SyntaxAdaptor, joinpath(path, "src/adaptor/SyntaxAdaptor.jl")) 13 | SimpleTypeChecker.API.addFile!(ctx, Wrapper.SimpleTypeChecker.SyntaxAdaptor, joinpath(path, "src/adaptor/JSAdaptor.jl")) 14 | SimpleTypeChecker.API.addFile!(ctx, Wrapper.SimpleTypeChecker.SyntaxAdaptor, joinpath(path, "src/adaptor/TreeQuery.jl")) 15 | SimpleTypeChecker.API.addFile!(ctx, Wrapper.SimpleTypeChecker.Inference, joinpath(path, "src/adaptor/InferenceDefinition.jl")) 16 | SimpleTypeChecker.API.addFile!(ctx, Wrapper.SimpleTypeChecker.Inference, joinpath(path, "src/adaptor/Inference.jl")) 17 | SimpleTypeChecker.API.addFile!(ctx, Wrapper.SimpleTypeChecker.Inference, joinpath(path, "src/adaptor/InferenceErrorUtility.jl")) 18 | SimpleTypeChecker.API.addFile!(ctx, Wrapper.SimpleTypeChecker.Inference, joinpath(path, "src/adaptor/InferenceError.jl")) 19 | SimpleTypeChecker.API.addFile!(ctx, Wrapper.SimpleTypeChecker.Inference, joinpath(path, "src/adaptor/ScopeChecking.jl")) 20 | SimpleTypeChecker.API.addFile!(ctx, Wrapper.SimpleTypeChecker.Server, joinpath(path, "src/server/SimpleHTTPServer.jl")) 21 | SimpleTypeChecker.API.runCheck!(ctx) 22 | end 23 | #include("eval.jl") -------------------------------------------------------------------------------- /test/runtests.jl: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ChenNingCong/SimpleTypeChecker/9222046080acd52f53e3a72fa7807c90c8e057cf/test/runtests.jl -------------------------------------------------------------------------------- /test/self/macro.jl: -------------------------------------------------------------------------------- 1 | Core.eval(SimpleTypeChecker.Inference, quote 2 | function expander_view(eng::Engine, ctx::Context, ast::JuAST)::InferResult 3 | args = FlowNode[] 4 | for i in ast.args 5 | rel = inferExpr(eng, ctx, i) 6 | ctx = rel.ctx 7 | push!(args, rel.node) 8 | end 9 | ms = MethodCallStruct(makeLiteralFlowNode(ast, makeConstJuASTVal(Base.maybeview)), args) 10 | mm = getMethodMatches(eng, ms) 11 | if length(mm) >= 2 || length(mm) == 0 12 | # TODO : report mismatched method table here 13 | reportErrorFunCall(eng, ast, ms, length(mm), false) 14 | end 15 | tt = extractUniqueMatch(mm) 16 | if isBottomType(tt) 17 | reportErrorBroadcastBottom(eng, ast, ms) 18 | end 19 | node = makeFunCallFlowNode(ast, ms, tt) 20 | addFlowMapping!(eng, ast, node) 21 | return InferResult(ctx, node) 22 | end 23 | 24 | function expander_nocheck(eng::Engine, ctx::Context, ast::JuAST)::InferResult 25 | InferResult(ctx, makeLiteralFlowNode(ast, makeConstJuASTVal(nothing))) 26 | end 27 | 28 | function expander_inline(eng::Engine, ctx::Context, ast::JuAST)::InferResult 29 | return inferExpr(ast.args[1]) 30 | end 31 | 32 | addGlobalExpander!(Base, Symbol("@view"), expander_view) 33 | addGlobalExpander!(Base, Symbol("@inline"), expander_inline) 34 | addGlobalExpander!(@__MODULE__, Symbol("@nocheck"), expander_nocheck) 35 | 36 | end) -------------------------------------------------------------------------------- /test/self/selftest.jl: -------------------------------------------------------------------------------- 1 | module MyTest 2 | 3 | include("../../src/SimpleTypeChecker.jl") 4 | include("macro.jl") 5 | const path = abspath(".") 6 | ctx = SimpleTypeChecker.Inference.GlobalContext() 7 | SimpleTypeChecker.Inference.getGlobalExpander(ctx) 8 | SimpleTypeChecker.API.addFile!(ctx, SimpleTypeChecker.SyntaxDefinition, joinpath(path, "src/adaptor/SyntaxDefinition.jl")) 9 | SimpleTypeChecker.API.addFile!(ctx, SimpleTypeChecker.SyntaxAdaptor, joinpath(path, "src/adaptor/SyntaxAdaptor.jl")) 10 | SimpleTypeChecker.API.addFile!(ctx, SimpleTypeChecker.SyntaxAdaptor, joinpath(path, "src/adaptor/JSAdaptor.jl")) 11 | SimpleTypeChecker.API.addFile!(ctx, SimpleTypeChecker.SyntaxAdaptor, joinpath(path, "src/adaptor/TreeQuery.jl")) 12 | SimpleTypeChecker.API.addFile!(ctx, SimpleTypeChecker.Inference, joinpath(path, "src/adaptor/InferenceDefinition.jl")) 13 | SimpleTypeChecker.API.addFile!(ctx, SimpleTypeChecker.Inference, joinpath(path, "src/adaptor/Inference.jl")) 14 | SimpleTypeChecker.API.addFile!(ctx, SimpleTypeChecker.Inference, joinpath(path, "src/adaptor/InferenceErrorUtility.jl")) 15 | SimpleTypeChecker.API.addFile!(ctx, SimpleTypeChecker.Inference, joinpath(path, "src/adaptor/InferenceError.jl")) 16 | SimpleTypeChecker.API.addFile!(ctx, SimpleTypeChecker.Inference, joinpath(path, "src/adaptor/ScopeChecking.jl")) 17 | SimpleTypeChecker.API.addFile!(ctx, SimpleTypeChecker.Server, joinpath(path, "src/server/SimpleHTTPServer.jl")) 18 | SimpleTypeChecker.API.runCheck!(ctx) 19 | 20 | end -------------------------------------------------------------------------------- /test/watch.jl: -------------------------------------------------------------------------------- 1 | import Dates 2 | if !isdefined(Main, :modtime) 3 | modtime = nothing 4 | global function watchDirectory(path::String) 5 | maxtime::Float64 = 0.0 6 | for (root, dirs, files) in walkdir(path) 7 | for file in files 8 | maxtime = max(mtime(joinpath(root, file)), maxtime) 9 | end 10 | end 11 | return Dates.unix2datetime(maxtime) 12 | end 13 | end 14 | newtime = watchDirectory(joinpath(@__DIR__, "../src/adaptor")) 15 | if modtime isa Nothing || modtime != newtime 16 | println("Update code $modtime $newtime") 17 | modtime = newtime 18 | include("eval.jl") 19 | end 20 | --------------------------------------------------------------------------------