shithub: libtroll

Download patch

ref: 7979dfca14134208fe237097f44a7a080e42b62f
parent: 27ae521c8504a1de9feac7e6183e47cda2d9a497
author: hexyl <hexyl@tuta.io>
date: Sun Jan 8 10:21:59 EST 2023

rewrite in go

diff: cannot open b/backends//null: file does not exist: 'b/backends//null' diff: cannot open a/src//null: file does not exist: 'a/src//null' diff: cannot open b/utils//null: file does not exist: 'b/utils//null'
--- a/README
+++ b/README
@@ -1,21 +1,18 @@
 libtroll
 --------
-libtroll is a tiny codegen library written in C, normally used in compilers & such
+libtroll is a tiny codegen library written in go, normally used in compilers & such
 libtroll isnt very optimized so be aware of that.
 
 plans
 -----
 the plans are in order of how ill make them
-[ ] generate portable C
 [ ] generate C++
 [ ] generate JS
-[ ] generate Python
 not everything is done
 
 usage
 -----
-plop src/libtroll.c & src/libtroll.h into your project
-make sure to compile the C file
+dont use it yet, thanks!
 
 license
 -------
--- /dev/null
+++ b/backend.go
@@ -1,0 +1,9 @@
+package libtroll
+
+type Backend interface {
+	Function() string
+	Variable() string
+	Constant() string
+	Struct() string
+	Finalize() string
+}
--- /dev/null
+++ b/backends/cpp.go
@@ -1,0 +1,24 @@
+package backends
+
+type BackendCPP struct {
+}
+
+func (b BackendCPP) Function() string {
+	return "not done"
+}
+
+func (b BackendCPP) Variable() string {
+	return "not done"
+}
+
+func (b BackendCPP) Constant() string {
+	return "not done"
+}
+
+func (b BackendCPP) Struct() string {
+	return "not done"
+}
+
+func (b BackendCPP) Finalize() string {
+	return "not done"
+}
--- /dev/null
+++ b/go.mod
@@ -1,0 +1,3 @@
+module shithub.us/hexyl/libtroll
+
+go 1.19
--- /dev/null
+++ b/libtroll.go
@@ -1,0 +1,38 @@
+package libtroll
+
+import (
+	"fmt"
+	"os"
+	"strconv"
+	"time"
+
+	"shithub.us/hexyl/libtroll/utils"
+)
+
+type Type int64
+
+type Program struct {
+	name    string
+	typ     Type
+	out     os.File
+	backend Backend
+}
+
+func (p *Program) Init(b Backend) {
+	timestamp := strconv.FormatInt(time.Now().UTC().UnixNano(), 10)
+	tmp := fmt.Sprintf("%s-%s.src", p.name, timestamp)
+	f, err := os.CreateTemp("", tmp)
+	utils.Check(err)
+	p.out = *f
+	p.backend = b
+	defer os.Remove(f.Name())
+}
+
+func New(name string, typ Type) Program {
+	tmpnew := Program{
+		name: name,
+		typ:  typ,
+	}
+
+	return tmpnew
+}
--- /dev/null
+++ b/libtroll_test.go
@@ -1,0 +1,13 @@
+package libtroll_test
+
+import (
+	"testing"
+
+	"shithub.us/hexyl/libtroll"
+	"shithub.us/hexyl/libtroll/backends"
+)
+
+func TestInit(t *testing.T) {
+	troll := libtroll.New("test", 0)
+	troll.Init(backends.BackendCPP{})
+}
--- a/mkfile
+++ /dev/null
@@ -1,10 +1,0 @@
-</$objtype/mkfile
-
-LIB=/$objtype/lib/libtroll.a
-OFILES=libtroll.$O
-HFILES=src/libtroll.h
-
-</sys/src/cmd/mksyslib
-
-install:V:	$LIB $HFILES
-	cp $HFILES /sys/include
--- a/src/libtroll.c
+++ /dev/null
@@ -1,1 +1,0 @@
-#include "libtroll.h"
\ No newline at end of file
--- a/src/libtroll.h
+++ /dev/null
@@ -1,5 +1,0 @@
-#ifndef LIBTROLL_H
-#define LIBTROLL_H
-
-
-#endif
\ No newline at end of file
--- /dev/null
+++ b/utils/utils.go
@@ -1,0 +1,7 @@
+package utils
+
+func Check(e error) {
+	if e != nil {
+		panic(e)
+	}
+}