Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add basic string interpolation / string templates #743

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions effekt/shared/src/main/scala/effekt/Compiler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,11 @@ trait Compiler[Executable] {
* - Server / Driver to typecheck and report type errors in VSCode
*/
def runFrontend(source: Source)(using C: Context): Option[Module] =
def getStackTrace(e: Throwable): String =
val stringWriter = new java.io.StringWriter()
e.printStackTrace(new java.io.PrintWriter(stringWriter))
stringWriter.toString

try {
val res = Frontend(source).map { res =>
val mod = res.mod
Expand All @@ -128,15 +133,11 @@ trait Compiler[Executable] {
None
case e @ CompilerPanic(msg) =>
C.report(msg)
e.getStackTrace.foreach { line =>
C.info(" at " + line)
}
C.info(getStackTrace(e))
None
case e =>
C.info("Effekt Compiler Crash: " + e.getMessage)
e.getStackTrace.foreach { line =>
C.info(" at " + line)
}
C.info("Effekt Compiler Crash: " + e.toString)
C.info(getStackTrace(e))
None
}

Expand Down
6 changes: 5 additions & 1 deletion libraries/common/stringbuffer.effekt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ def stringBuffer[A] { prog: => A / StringBuffer }: A = {
else {
// Double the capacity while ensuring the required capacity
val newSize = max(buffer.size * 2, buffer.size + sizeToAdd)
buffer = bytearray::resize(buffer, newSize)
val newBuffer = bytearray::allocate(newSize)
buffer = buffer.resize(newSize)
dvdvgt marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand All @@ -35,7 +36,10 @@ def stringBuffer[A] { prog: => A / StringBuffer }: A = {
resume(())
}
def flush() = {
// resize buffer to strip trailing zeros that otherwise would be converted into 0x00 characters
buffer = bytearray::resize(buffer, pos)
val str = buffer.toString()
// after flushing, the stringbuffer should be empty again
buffer = bytearray::allocate(initialCapacity)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this could be made somewhat more efficient -- resize copies into a newly allocated bytearray, but toString doesn't (since on LLVM strings are bytearrays)
So it should be possible to just clear the buffer without allocating it again?

Suggested change
// resize buffer to strip trailing zeros that otherwise would be converted into 0x00 characters
buffer = bytearray::resize(buffer, pos)
val str = buffer.toString()
// after flushing, the stringbuffer should be empty again
buffer = bytearray::allocate(initialCapacity)
// resize buffer to strip trailing zeros that otherwise would be converted into 0x00 characters
val str = bytearray::resize(buffer, pos).toString()
// after flushing, the stringbuffer should be empty again
buffer = buffer.fill(0)

where buffer.fill(0) means memset(buffer, 0) (which we don't have yet) :)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, you could just clear the buffer, however, then the buffer's size will not be reset to a (potentially) smaller size and instead just buffer's previous size is kept. This probably would not be a big deal, though.

resume(str)
}
Expand Down
Loading