prototype repl
This commit is contained in:
parent
30481d4f78
commit
c40aea7326
7 changed files with 233 additions and 35 deletions
|
|
@ -17,6 +17,8 @@
|
|||
|
||||
package ast
|
||||
|
||||
import "git.callpipe.com/aidan/shs/log"
|
||||
|
||||
func (t *Token) Eval(funcs FuncTable, vars VarTable) *Token {
|
||||
if t == nil {
|
||||
return nil
|
||||
|
|
@ -53,7 +55,9 @@ func (t *Token) Eval(funcs FuncTable, vars VarTable) *Token {
|
|||
f := funcs.GetFunction(ret.Inner.(string))
|
||||
if f == nil {
|
||||
if !eligibleForSystemCall {
|
||||
// TODO: log error
|
||||
log.Log(log.DEBUG,
|
||||
"could not find definition for symbol " + ret.Inner.(string),
|
||||
"eval")
|
||||
return nil
|
||||
}
|
||||
|
||||
|
|
@ -61,7 +65,7 @@ func (t *Token) Eval(funcs FuncTable, vars VarTable) *Token {
|
|||
return nil // TODO: Thats gotta change
|
||||
}
|
||||
|
||||
return (*f).CallFunction(ret.Next)
|
||||
return (*f).CallFunction(ret.Next, vars, funcs)
|
||||
}
|
||||
|
||||
return ret
|
||||
|
|
|
|||
|
|
@ -21,6 +21,7 @@ type Operation func(*Token) *Token
|
|||
|
||||
type Function struct {
|
||||
function Operation
|
||||
name string
|
||||
timesCalled int
|
||||
args int // TODO: Make this a list of expected types (TAGs)
|
||||
}
|
||||
|
|
@ -40,7 +41,9 @@ func (f Function) ParseFunction(args *Token) bool {
|
|||
}
|
||||
|
||||
if i != 0 {
|
||||
// TODO: log error here
|
||||
log.Log(log.ERR,
|
||||
"Incorrect number of arguments",
|
||||
"eval")
|
||||
return false
|
||||
}
|
||||
|
||||
|
|
@ -49,6 +52,9 @@ func (f Function) ParseFunction(args *Token) bool {
|
|||
|
||||
func (f Function) CallFunction(args *Token) *Token {
|
||||
if !f.ParseFunction(args) {
|
||||
log.Log(log.Err,
|
||||
"Couldnt call " + f.name,
|
||||
"eval")
|
||||
return nil
|
||||
}
|
||||
|
||||
|
|
@ -59,6 +65,9 @@ func (f Function) CallFunction(args *Token) *Token {
|
|||
func (table FuncTable) GetFunction(arg string) *Function {
|
||||
target, ok := table[arg]
|
||||
if !ok {
|
||||
log.Log(log.DEBUG,
|
||||
"function " + arg + " not found",
|
||||
"eval")
|
||||
return nil
|
||||
}
|
||||
|
||||
|
|
|
|||
12
ast/token.go
12
ast/token.go
|
|
@ -164,11 +164,17 @@ error:
|
|||
// TODO: Hook into error module
|
||||
// TODO: Finalize and GC alloced tokens
|
||||
if start_pos == -1 {
|
||||
println("[-] Unmatched string delimiter in input. discarding.")
|
||||
log.Log(log.ERR,
|
||||
"Unmatched string delimiter in input. discarding.",
|
||||
"lex")
|
||||
} else if start_pos == -2 {
|
||||
println("[-] Unmatched list delimiter in input. discarding.")
|
||||
log.Log(log.ERR,
|
||||
"Unmatched list delimiter in input. discarding.",
|
||||
"lex")
|
||||
} else {
|
||||
println("[-] Unknown error in input. discarding.")
|
||||
log.Log(log.ERR,
|
||||
"Unknown error in input. discarding.",
|
||||
"lex")
|
||||
}
|
||||
|
||||
return nil
|
||||
|
|
|
|||
|
|
@ -20,10 +20,10 @@ package main
|
|||
import (
|
||||
"strings"
|
||||
"os"
|
||||
"git.callpipe.com/aidan/shs/io"
|
||||
"git.callpipe.com/aidan/shs/log"
|
||||
"git.callpipe.com/aidan/shs/ast"
|
||||
)
|
||||
|
||||
func main() {
|
||||
io.PrintSExpression(ast.Lex(strings.Join(os.Args[1:], " ")))
|
||||
log.PrintSExprsIndividually(ast.Lex(strings.Join(os.Args[1:], " ")))
|
||||
}
|
||||
|
|
|
|||
106
cmd/repl.go
Normal file
106
cmd/repl.go
Normal file
|
|
@ -0,0 +1,106 @@
|
|||
/* SHS: Syntactically Homogeneous Shell
|
||||
* Copyright (C) 2019 Aidan Hahn
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"bufio"
|
||||
"strconv"
|
||||
"git.callpipe.com/aidan/shs/ast"
|
||||
"git.callpipe.com/aidan/shs/log"
|
||||
)
|
||||
|
||||
const (
|
||||
def_prompt string = "λ"
|
||||
)
|
||||
|
||||
func setLogLvl() {
|
||||
loglvl := os.Getenv("SH_LOGGING")
|
||||
if loglvl != "" {
|
||||
llvl, ok := strconv.ParseInt(loglvl, 10, 8)
|
||||
if !ok {
|
||||
log.Log(log.Err,
|
||||
"couldnt parse log level",
|
||||
"init")
|
||||
} else {
|
||||
log.SetLogLvl(llvl)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
debug := os.Getenv("SH_DEBUG_MODE")
|
||||
hist := os.Getenv("SH_HIST_FILE")
|
||||
prompt := os.Getenv("SHS_SH_PROMPT")
|
||||
|
||||
var vars ast.VarTable
|
||||
var funcs ast.VarTable
|
||||
|
||||
useHist := false
|
||||
var histFile os.File
|
||||
var err error
|
||||
|
||||
if hist != "" {
|
||||
useHist = true
|
||||
histFile, err = os.OpenFile(histFile, os.O_RDWR|os.O_CREATE, 0755)
|
||||
if err != nil {
|
||||
useHist = false
|
||||
log.Log(log.ERR, "coudlnt open histfile: " + err, "init")
|
||||
}
|
||||
}
|
||||
|
||||
if prompt == "" {
|
||||
prompt = def_prompt
|
||||
}
|
||||
|
||||
// TODO: is bufio right for this?
|
||||
reader := bufio.NewReader(os.Stdin)
|
||||
for {
|
||||
setLogLvl()
|
||||
fmt.Print(prompt + " ")
|
||||
text, err := reader.ReadString('\n')
|
||||
if err != nil {
|
||||
log.Log(log.ERR, "couldnt read user input: " + err, "repl")
|
||||
}
|
||||
|
||||
// TODO: replace with a regex
|
||||
text = strings.Replace(text, "\r\n", "", -1)
|
||||
text = strings.Replace(text, "\n", "", -1)
|
||||
|
||||
if useHist {
|
||||
_, err = histFile.Write([]byte(text + "\n"))
|
||||
if err != nil {
|
||||
log.Log(log.DEBUG, "couldnt write to histfile: " + err, "repl")
|
||||
}
|
||||
}
|
||||
|
||||
userInput := ast.Lex(text)
|
||||
if userInput == nil {
|
||||
// errors handled in Lex
|
||||
// TODO: return an error instead
|
||||
continue
|
||||
}
|
||||
|
||||
if debug != "" {
|
||||
log.PrintSExprsIndividually(userInput)
|
||||
}
|
||||
|
||||
result := userInput.Eval(funcs, vars)
|
||||
fmt.Println(result.String() + "\n")
|
||||
}
|
||||
}
|
||||
44
log/logger.go
Normal file
44
log/logger.go
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
/* SHS: Syntactically Homogeneous Shell
|
||||
* Copyright (C) 2019 Aidan Hahn
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package log
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
const (
|
||||
ERR int = 0
|
||||
DEBUG int = 1
|
||||
INFO int = 2
|
||||
)
|
||||
|
||||
var logLevel int
|
||||
|
||||
func SetLogLvl(lvl int) {
|
||||
if lvl < 4 && lvl > 0 {
|
||||
logLevel = lvl
|
||||
}
|
||||
}
|
||||
|
||||
func Log(lvl int, msg, context string) {
|
||||
if lvl > logLevel {
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Println("[" + context + "] " + msg)
|
||||
}
|
||||
|
|
@ -15,7 +15,7 @@
|
|||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package io
|
||||
package log
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
|
@ -24,6 +24,61 @@ import (
|
|||
"git.callpipe.com/aidan/shs/datatypes"
|
||||
)
|
||||
|
||||
/* Print function which is better suited for repl.
|
||||
* This one prints the SEXPRs as one would write them.
|
||||
*/
|
||||
func (t *ast.Token) String() string {
|
||||
switch tag {
|
||||
case ast.STRING
|
||||
return "\"" + t.Inner.(string) + "\""
|
||||
|
||||
case ast.NUMBER:
|
||||
return t.Inner.(string)
|
||||
|
||||
case ast.LIST:
|
||||
repr := "("
|
||||
for i := t.Inner.(*ast.Token); i != nil; i = i.Next {
|
||||
repr = repr + i.String() + " "
|
||||
}
|
||||
return repr + ")"
|
||||
|
||||
case ast.SYMBOL:
|
||||
return "<" + t.Inner.(string) + ">"
|
||||
}
|
||||
}
|
||||
|
||||
/* Print function which breaks each embedded list out on individual lines.
|
||||
* Used in the print_ast debug tool. not too useful for repl applications.
|
||||
* Very useful for debugging syntax though.
|
||||
* TODO: Add numbers to denote embedded scope?
|
||||
*/
|
||||
func PrintSExprsIndividually(arg *ast.Token) {
|
||||
if arg == nil {
|
||||
return //TODO: Handle error here?
|
||||
}
|
||||
|
||||
var lists datatypes.TokenStack;
|
||||
lists.Push(arg)
|
||||
|
||||
loop:
|
||||
var constructor strings.Builder
|
||||
i := lists.Pop()
|
||||
if i == nil {
|
||||
return
|
||||
}
|
||||
|
||||
for iter := i; iter != nil; iter = iter.Next {
|
||||
if iter.Tag == ast.LIST {
|
||||
lists.Push(iter.Inner.(*ast.Token))
|
||||
}
|
||||
|
||||
constructor.WriteString(FmtToken(iter))
|
||||
}
|
||||
|
||||
println(constructor.String())
|
||||
goto loop
|
||||
}
|
||||
|
||||
func FmtToken(arg *ast.Token) string {
|
||||
suffix := "->"
|
||||
if arg.Next == nil {
|
||||
|
|
@ -53,29 +108,3 @@ func GetTagAsStr(tag ast.Token_t) string {
|
|||
return "UNKNOWN"
|
||||
}
|
||||
|
||||
func PrintSExpression(arg *ast.Token) {
|
||||
if arg == nil {
|
||||
return //TODO: Handle error here?
|
||||
}
|
||||
|
||||
var lists datatypes.TokenStack;
|
||||
lists.Push(arg)
|
||||
|
||||
loop:
|
||||
var constructor strings.Builder
|
||||
i := lists.Pop()
|
||||
if i == nil {
|
||||
return
|
||||
}
|
||||
|
||||
for iter := i; iter != nil; iter = iter.Next {
|
||||
if iter.Tag == ast.LIST {
|
||||
lists.Push(iter.Inner.(*ast.Token))
|
||||
}
|
||||
|
||||
constructor.WriteString(FmtToken(iter))
|
||||
}
|
||||
|
||||
println(constructor.String())
|
||||
goto loop
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue