Skip to content

Commit

Permalink
feat(compiler): add execution logic for variable pattern functions #16
Browse files Browse the repository at this point in the history
Implemented the execution logic for variable pattern functions in the VariablePatternFunc class. Also updated the SymbolResolver class to execute variable functions with the given key and pipeline functions.
  • Loading branch information
phodal committed Jun 18, 2024
1 parent 411ebf4 commit 3542461
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,42 @@
package com.phodal.shirelang.compiler.hobbit

class VariablePatternFunc(val name: String, val function: PatternFun)
class VariablePatternFunc(val name: String, val function: PatternFun) {
fun execute(input: Any): String {
var result = input
when(function) {
is PatternFun.Prompt -> {
result = function.message
}
is PatternFun.Grep -> {
result = function.patterns.joinToString("\n")
}
is PatternFun.Sed -> {
result = (result as String).replace(function.pattern.toRegex(), function.replacements)
}
is PatternFun.Sort -> {
result = function.arguments.sorted().joinToString("\n")
}
is PatternFun.Uniq -> {
result = function.texts.distinct().joinToString("\n")
}
is PatternFun.Head -> {
result = (result as String).split("\n").take(function.lines.toInt()).joinToString("\n")
}
is PatternFun.Tail -> {
result = (result as String).split("\n").takeLast(function.lines.toInt()).joinToString("\n")
}

is PatternFun.Cat -> {
result = function.paths.joinToString("\n")
}
is PatternFun.Print -> {
result = function.texts.joinToString("\n")
}
is PatternFun.Xargs -> {
result = function.variables
}
}

return result.toString()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ class SymbolResolver(val myProject: Project, val editor: Editor, val hole: Hobbi

results.putAll(SystemInfoVariable.resolve())

// hole?.variables?.forEach {
// results[it.key] = VariableFuncExecutor.execute(it.value)
// }
hole?.variables?.forEach {
results[it.key] = VariableFuncExecutor.execute(it.key, it.value)
}

return results
}
Expand Down Expand Up @@ -74,8 +74,18 @@ class SymbolResolver(val myProject: Project, val editor: Editor, val hole: Hobbi

class VariableFuncExecutor {
companion object {
fun execute(value: List<VariablePatternFunc>): String {
TODO("Not yet implemented")
/**
* We should execute the variable function with the given key and pipeline functions.
*
* Each function output will be the input of the next function.
*/
fun execute(key: String, pipelineFuncs: List<VariablePatternFunc>): String {
var result = key
pipelineFuncs.forEach {
result = it.execute(result)
}

return result
}
}

Expand Down

0 comments on commit 3542461

Please sign in to comment.