Skip to content

Commit

Permalink
split out diagnostics to reduce dependecy on truffle for org.intellig…
Browse files Browse the repository at this point in the history
…ence.*
  • Loading branch information
ekmett committed Nov 2, 2019
1 parent 04fcf4a commit ecaa7cb
Show file tree
Hide file tree
Showing 7 changed files with 180 additions and 148 deletions.
2 changes: 1 addition & 1 deletion src/asm/types.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ val double: Type get() = Type.DOUBLE_TYPE
val boolean: Type get() = Type.BOOLEAN_TYPE
fun type(k: KClass<*>): Type = Type.getType(k.java)
fun type(t: String): Type = Type.getObjectType(t)
val Type.array: Type get() = Type.getType("[${descriptor}")
val Type.array: Type get() = Type.getType("[$descriptor")
val string: Type get() = +String::class
val `object`: Type get() = +Object::class
val `class`: Type get() = +Class::class
Expand Down
11 changes: 5 additions & 6 deletions src/cadenza/language.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package cadenza

import cadenza.semantics.Type.*
import cadenza.data.Closure
import cadenza.jit.Code
import cadenza.jit.InlineCode
import cadenza.jit.ProgramRootNode
import cadenza.semantics.Term
import cadenza.semantics.Type
import cadenza.semantics.Type.Arr
import cadenza.semantics.Type.Nat
import com.oracle.truffle.api.*
import com.oracle.truffle.api.TruffleLanguage.ContextPolicy
import com.oracle.truffle.api.debug.DebuggerTags
Expand All @@ -18,7 +19,8 @@ import com.oracle.truffle.api.interop.TruffleObject
import com.oracle.truffle.api.interop.UnsupportedMessageException
import com.oracle.truffle.api.nodes.NodeInfo
import com.oracle.truffle.api.source.SourceSection
import org.graalvm.options.*
import org.graalvm.options.OptionDescriptors
import org.graalvm.options.OptionValues
import org.graalvm.polyglot.Source
import java.io.IOException
import java.nio.charset.Charset
Expand Down Expand Up @@ -120,9 +122,6 @@ class Language : TruffleLanguage<Language.Context>() {
fun shutdown() {}
}




private val singleContextAssumption = Truffle.getRuntime().createAssumption("Only a single context is active")!!
override fun createContext(env: Env) = Context(this, env)
override fun initializeContext(ctx: Context?) {}
Expand Down Expand Up @@ -153,7 +152,7 @@ class Language : TruffleLanguage<Language.Context>() {
// stubbed: returns a calculation that adds two numbers
override fun parse(request: ParsingRequest): CallTarget {
val rootNode = ProgramRootNode(this, Code.LitInt(0), FrameDescriptor())
//panic("at the disco")
// todo
return Truffle.getRuntime().createCallTarget(rootNode)
}

Expand Down
5 changes: 3 additions & 2 deletions src/cadenza/launcher.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ internal fun PolyglotException.prettyStackTrace(trim: Boolean = true) {
}
}
val out = ansi()
if (isHostException) out.fgRed().a(asHostException().toString())
else out.fgBrightYellow().a(message)
//if (isHostException) out.fgRed().a(asHostException().toString())
//else out.fgBrightYellow().a(message)
out.a(if (isHostException) asHostException().toString() else message)
out.reset().a('\n').fgBrightBlack()
stackTrace.forEach {
out.a(Attribute.ITALIC).a(" at ").a(Attribute.ITALIC_OFF).a(it).a('\n')
Expand Down
22 changes: 16 additions & 6 deletions src/cadenza/panic.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package cadenza

import com.oracle.truffle.api.CompilerDirectives
import org.intelligence.diagnostics.Severity
import org.intelligence.diagnostics.error
import org.intelligence.pretty.Pretty

private inline fun <reified T> Array<T>.trim(i : Int = 1): Array<T> = this.drop(i).toTypedArray()

Expand All @@ -9,7 +12,11 @@ internal class Panic(message: String? = null) : RuntimeException(message) {
initCause(cause)
}
companion object { const val serialVersionUID : Long = 1L }
override fun toString(): String = if (message.isNullOrEmpty()) "panic" else "panic: $message"
override fun toString(): String = stackTrace?.getOrNull(0)?.let {
Pretty.ppString {
error(Severity.panic, it.fileName, it.lineNumber, null, null, message)
}
} ?: super.toString()
}

fun panic(msg: String, base: Throwable?): Nothing {
Expand All @@ -23,15 +30,18 @@ fun panic(msg: String): Nothing {
throw Panic(msg).also { it.stackTrace = it.stackTrace.trim() }
}

internal class TODO private constructor() : RuntimeException() {
internal class TODO() : RuntimeException() {
companion object { const val serialVersionUID : Long = 1L }
override fun toString(): String = stackTrace[0].let {
"${it.fileName}:${it.lineNumber}: todo (${it.methodName})"
}
override fun toString(): String =
stackTrace?.getOrNull(0)?.let {
Pretty.ppString {
error(Severity.todo, it.fileName, it.lineNumber, null, null, it.methodName)
}
} ?: super.toString()
}

val todo: Nothing get() {
CompilerDirectives.transferToInterpreter()
throw RuntimeException().also { it.stackTrace = it.stackTrace.trim() }
throw TODO().also { it.stackTrace = it.stackTrace.trim() }
}

48 changes: 48 additions & 0 deletions src/diagnostics.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package org.intelligence.diagnostics

import com.oracle.truffle.api.source.Source
import org.fusesource.jansi.Ansi
import org.intelligence.pretty.*

enum class Severity { info, warning, error, todo, panic }

fun Pretty.error(severity: Severity = Severity.error, file: String, line: Int, col: Int? = null, sourceLine: CharSequence? = null, message: String? = null, vararg expected: Any) {
bold {
text(file); char(':'); simple(line); char(':');
col?.also { simple(it); char(':') };
space
when (severity) {
Severity.error -> red { text("error:") }
Severity.panic -> fg(Ansi.Color.RED, true) { text("panic:") }
Severity.warning -> magenta { text("warning:") }
Severity.info -> blue { text("info:") }
Severity.todo -> yellow { text("todo:")}
}
space
nest(2) {
if (expected.isEmpty()) text(message ?: "expected nothing")
else {
if (message != null) {
text(message);text(",");space
}
text("expected")
space
oxfordBy(by = Pretty::simple, conjunction = "or", docs = *expected)
}
}
}
sourceLine?.also {
hardLine
text(it)
col?.also { nest(it - 1) { newline; cyan { char('^') } } }
}
}

// convenient pretty printer
fun Pretty.error(severity: Severity = Severity.error, source: Source, pos: Int, message: String? = null, vararg expected: Any) {
val line = source.getLineNumber(pos)
val ls = source.getLineStartOffset(line)
val ll = source.getLineLength(line)
val sourceText = source.characters.subSequence(ls, ls+ll)
error(severity, source.name, line, source.getColumnNumber(pos), sourceText, message, *expected)
}
Loading

0 comments on commit ecaa7cb

Please sign in to comment.