Skip to content

Commit

Permalink
Add logging, default Lua file, node prev/next
Browse files Browse the repository at this point in the history
  • Loading branch information
pgundlach committed Nov 2, 2021
1 parent e89373b commit 6892c6e
Show file tree
Hide file tree
Showing 11 changed files with 453 additions and 65 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
.vscode
*.code-workspace
dist/
store/
playground/
bin/
26 changes: 26 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
@versions = {}

File.read("version").each_line do |line|
product,versionnumber = line.chomp.split(/=/)
@versions[product]=versionnumber
end

@ets_version = @versions['ets_version']

desc "Show rake description"
task :default do
puts
puts "Run 'rake -T' for a list of tasks."
puts
puts "1: Use 'rake build' to build the 'ets' binary. That should be\n the starting point."
puts
end


desc "Compile and install necessary software"
task :build do
sh "go build -ldflags \"-X main.version=#{@ets_version}\" -o bin/ets github.com/speedata/ets/ets/ets"
end



17 changes: 9 additions & 8 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,25 @@
This software repository contains a Lua frontend for the typesetting library [“Boxes and Glue”](https://github.com/speedata/boxesandglue) which is an algorithmic typesetting machine in the spirits of TeX.


## Build
## Running the software

You just need Go installed on your system, clone the repository and run
Download the software ([get the latest release here](https://github.com/speedata/ets/releases)), unzip the archive and call

go build -o bin/ets github.com/speedata/ets/ets/ets
bin/ets myscript.lua

on the command line.

## Running the software
## Build

call
You just need Go installed on your system, clone the repository and run

go build -o bin/ets github.com/speedata/ets/ets/ets

bin/ets myscript.lua

on the command line

## Status

This software is more or less a demo of the architecture and usable for any serious purpose yet.
This software is more or less a demo of the architecture and not usable for any serious purpose yet.

Feedback welcome!

Expand Down
40 changes: 38 additions & 2 deletions core/core.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,51 @@
package core

import (
"os"
"path/filepath"

"github.com/speedata/boxesandglue/backend/bag"
lua "github.com/yuin/gopher-lua"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)

// Dothings opens the Lua file and executes it
func Dothings(luafile string) error {
// Runs the Lua file if it exists. If the exename is "foo", it looks for a Lua file called "foo.lua"
func runDefaultLua(l *lua.LState, exename string) error {
var extension = filepath.Ext(exename)
var name = exename[0:len(exename)-len(extension)] + ".lua"
var err error
if _, err = os.Stat(name); err != nil {
return nil
}
return l.DoFile(name)
}

func newZapLogger() *zap.SugaredLogger {
logger, _ := zap.Config{
Level: zap.NewAtomicLevelAt(zapcore.DebugLevel),
Encoding: "console",
OutputPaths: []string{"stdout"},
EncoderConfig: zapcore.EncoderConfig{
EncodeLevel: zapcore.LowercaseColorLevelEncoder,
LevelKey: "level",
MessageKey: "message",
},
}.Build()
return logger.Sugar()
}

// Dothings opens the Lua file and executes it
func Dothings(luafile string, exename string) error {
l := lua.NewState()
bag.Logger = newZapLogger()
registerDocumentType(l)
registerNodeType(l)

if err := runDefaultLua(l, exename); err != nil {
return err
}

if err := l.DoFile(luafile); err != nil {
return err
}
Expand Down
41 changes: 22 additions & 19 deletions core/document.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const (
func registerDocumentType(l *lua.LState) {
mt := l.NewTypeMetatable(luaDocumentTypeName)
l.SetGlobal("document", mt)
l.SetField(mt, "info", l.NewFunction(documentInfo))
l.SetField(mt, "new", l.NewFunction(newDocument))
l.SetField(mt, "sp", l.NewFunction(documentSP))
l.SetField(mt, "__index", l.NewFunction(indexDoc))
Expand All @@ -35,27 +36,39 @@ func registerDocumentType(l *lua.LState) {
// Constructor
func newDocument(l *lua.LState) int {
doc := &doc{}
lv := l.Get(-1)
filename := l.CheckString(1)
var w *os.File
var err error
if str, ok := lv.(lua.LString); ok {
w, err = os.Create(string(str))
if err != nil {
return lerr(l, err.Error())
}
} else {
l.TypeError(1, lua.LTString)
return 0
w, err = os.Create(filename)
if err != nil {
return lerr(l, err.Error())
}
doc.w = w
doc.d = document.NewDocument(w)
doc.d.Filename = filename
ud := l.NewUserData()
ud.Value = doc
l.SetMetatable(ud, l.GetTypeMetatable(luaDocumentTypeName))
l.Push(ud)
return 1
}

func documentSP(l *lua.LState) int {
arg := l.CheckString(1)
size, err := bag.Sp(arg)
if err != nil {
return lerr(l, err.Error())
}
l.Push(lua.LNumber(size))
return 1
}

func documentInfo(l *lua.LState) int {
str := l.CheckString(1)
bag.Logger.Info(str)
return 0
}

func indexDoc(l *lua.LState) int {
doc := checkDocument(l, 1)
switch arg := l.CheckString(2); arg {
Expand Down Expand Up @@ -115,16 +128,6 @@ func documentFinish(d *doc) lua.LGFunction {
}
}

func documentSP(l *lua.LState) int {
arg := l.CheckString(1)
size, err := bag.Sp(arg)
if err != nil {
return lerr(l, err.Error())
}
l.Push(lua.LNumber(size))
return 1
}

func documentLoadPatternFile(doc *document.Document) lua.LGFunction {
return func(l *lua.LState) int {
fn := l.CheckString(1)
Expand Down
Loading

0 comments on commit 6892c6e

Please sign in to comment.