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

Implement NAME statement #101

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ stmt
| functionreturnstmt
| functionendstmt
| importstmt
| killstmt
| namestmt
| libtagstmt
| dimstmt
| reallocstmt
Expand Down Expand Up @@ -372,6 +374,14 @@ importstmt
: IMPORT filename=string
;

namestmt
: NAME oldfilename=expr AS newfilename=expr
;

killstmt
: KILL filespec=expr
;

libtagstmt
: LIBTAG tag=string
;
Expand Down Expand Up @@ -743,6 +753,14 @@ FUNCTION
: F U N C T I O N
;

NAME
: N A M E
;

KILL
: K I L L
;

LIBTAG
: L I B T A G
;
Expand Down
49 changes: 48 additions & 1 deletion libbababasic/src/main/java/io/atha/libbababasic/file/BBFiles.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ import io.atha.libbababasic.error.RuntimeError
import io.atha.libbababasic.file.BBFile.FileAccessMode
import io.atha.libbababasic.file.BBFile.FileOpenMode
import io.atha.libbababasic.runtime.Environment
import java.nio.file.FileVisitOption
import java.nio.file.FileVisitResult
import java.nio.file.Files
import java.nio.file.SimpleFileVisitor
import java.nio.file.attribute.BasicFileAttributes
import java.nio.file.Path
import kotlin.io.path.Path
import kotlin.io.path.PathWalkOption

class BBFiles(@JvmField val sys: BBUIFile, val env: Environment) {
private val files: MutableMap<Int, BBFile> = mutableMapOf()
Expand Down Expand Up @@ -80,4 +88,43 @@ class BBFiles(@JvmField val sys: BBUIFile, val env: Environment) {
}
}
}
}

fun kill(filespec: String) {
// TODO: implement
// delete files from the storage folder that match filespec
val storageFolder = getMappedPath("")
val matcher = FileMatcher(filespec)
Files.walkFileTree(
Path(storageFolder),
setOf(FileVisitOption.FOLLOW_LINKS),
Int.MAX_VALUE,
matcher
)

matcher.matchedFiles.forEach { file ->
try {
Files.delete(file)
println("Deleted file: $file")
} catch (e: Exception) {
println("Failed to delete file: $file")
e.printStackTrace()
}
}
}
}

class FileMatcher(private val spec: String) : SimpleFileVisitor<Path>() {
val matchedFiles = mutableListOf<Path>()

override fun visitFile(file: Path?, attrs: BasicFileAttributes?): FileVisitResult {
if (file != null && Files.isRegularFile(file) && matchesSpec(file.fileName.toString(), spec)) {
matchedFiles.add(file)
}
return FileVisitResult.CONTINUE
}
}

fun matchesSpec(fileName: String, spec: String): Boolean {
return fileName.matches(spec.replace(".", "\\.").replace("*", ".*").toRegex())
}

Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ class IR(@JvmField val symbolTable: SymbolTable) {
MOUSEBUTTONPRESSED("mousebuttonpressed"), MOUSEBUTTONRELEASED("mousebuttonreleased"), ISKEYPRESSED(
"iskeypressed"
),
LTRIMDLR("ltrim$"), RTRIMDLR("rtrim$"), DATEDLR("date$"), TIMEDLR("date$")
LTRIMDLR("ltrim$"), RTRIMDLR("rtrim$"), DATEDLR("date$"), TIMEDLR("date$"), KILL("kill"), NAME("name")
}

class InputRef(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4938,6 +4938,27 @@ class IRListener(
)
}

override fun exitNamestmt(ctx: BabaBASICParser.NamestmtContext) {
val oldfilenameExpr = lookupInstruction(ctx.expr())
Types.assertString(ir.symbolTable[oldfilenameExpr.result]!!.type!!.atomTypeId) { getCtxString(ctx) }
val newfilenameExpr = lookupInstruction(ctx.expr())
Types.assertString(ir.symbolTable[newfilenameExpr.result]!!.type!!.atomTypeId) { getCtxString(ctx) }
// TODO
ir.addInstruction(
sourceFile, currentLineNumber, ctx.start.startIndex, ctx.stop.stopIndex,
OpCode.NAME, SymbolTable.NULL_ID, oldfilenameExpr.result, newfilenameExpr.result
)
}

override fun exitKillstmt(ctx: BabaBASICParser.KillstmtContext) {
val filespecExpr = lookupInstruction(ctx.expr())
Types.assertString(ir.symbolTable[filespecExpr.result]!!.type!!.atomTypeId) { getCtxString(ctx) }
ir.addInstruction(
sourceFile, currentLineNumber, ctx.start.startIndex, ctx.stop.stopIndex,
OpCode.KILL, filespecExpr.result, SymbolTable.NULL_ID, SymbolTable.NULL_ID
)
}

private fun assertGraphics() {
if (!graphics) {
throw InternalError(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ import io.atha.libbababasic.runtime.Functions.fnint
import io.atha.libbababasic.runtime.Functions.hexdlr
import io.atha.libbababasic.runtime.Functions.inputdlr
import io.atha.libbababasic.runtime.Functions.instr
import io.atha.libbababasic.runtime.Functions.kill
import io.atha.libbababasic.runtime.Functions.leftdlr
import io.atha.libbababasic.runtime.Functions.len
import io.atha.libbababasic.runtime.Functions.loc
Expand Down Expand Up @@ -771,6 +772,7 @@ class BBRuntime(

OpCode.LTRIMDLR -> ltrimdlr(ir.symbolTable, instruction)
OpCode.RTRIMDLR -> rtrimdlr(ir.symbolTable, instruction)
OpCode.KILL -> kill(files!!, ir.symbolTable, instruction)
OpCode.COMMENT -> {}
OpCode.VARIABLE -> {}
OpCode.VALUE -> {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,12 @@ object Functions {
symbolTable[instruction.result]!!.value!!.string = rtrim
}

@JvmStatic
fun kill(files: BBFiles, symbolTable: SymbolTable, instruction: IR.Instruction) {
var filespec = symbolTable[instruction.op1]!!.value!!.string
files.kill(filespec!!)
}

@JvmStatic
fun `val`(symbolTable: SymbolTable, instruction: IR.Instruction) {
val str = symbolTable[instruction.op1]!!.value!!.string
Expand Down