'unexpected fault address' When Calling EnumProcessModules - go

I am writing a function that enumerates the base address of a process. This is done through a few Windows API calls, however, I believe it's my call to EnumProcessModules that is creating the error.
It should also be noted that this error does not occur for every process.
I have already tried changing around some of the uint32 to uint64 and vice versa, and different ways of creating the moduleHandles array, but I can't get anything working.
This is just one function in an application I'm creating to retrieve and scan another process's memory.
This is the source code of my function:
func getBaseAddress(handle uintptr) int64 {
// GetProcessImageFileNameA
var imageFileName [200]byte
var fileSize uint32 = 200
var fileName string
ret, _, _ := procGetProcessImageFileNameA.Call(handle, uintptr(unsafe.Pointer(&imageFileName)), uintptr(fileSize))
for _, char := range imageFileName {
if char == 0 {
break
}
fileName += string(char)
}
fileName = fileName[24:]
// EnumProcessModules
var n uint32
var needed uint64
ret, _, _ = procEnumProcessModules.Call(handle, 0, uintptr(n), uintptr(unsafe.Pointer(&needed)))
moduleHandles := make([]syscall.Handle, int(needed))
if ret == 1 && needed > 0 {
ret, _, _ = procEnumProcessModules.Call(handle, uintptr(unsafe.Pointer(&moduleHandles)), uintptr(needed), uintptr(unsafe.Pointer(&needed)))
}
// GetModuleFileNameExA
var finalModuleHandle uintptr
for _, moduleHandle := range moduleHandles {
if moduleHandle > 0 {
var moduleFileName [200]byte
var moduleSize uint32 = 200
var moduleName string
ret, _, _ = procGetModuleFileNameExA.Call(handle, uintptr(moduleHandle), uintptr(unsafe.Pointer(&moduleFileName)), uintptr(moduleSize))
if ret != 0 {
for _, char := range moduleFileName {
if char == 0 {
break
}
moduleName += string(char)
}
moduleName = moduleName[3:]
if moduleName == fileName {
finalModuleHandle = uintptr(moduleHandle)
break
}
}
}
}
return int64(finalModuleHandle)
}

The 2nd parameter to EnumProcessModules cannot be a null pointer (0) even if you are just trying to determine the number of entries needed.

I figured out the issue. I was interacting with the EnumProcessModules incorrectly. Here's a working function:
func getBaseAddress(handle uintptr) int64 {
// GetProcessImageFileNameA
var imageFileName [200]byte
var fileSize uint32 = 200
var fileName string
ret, _, _ := procGetProcessImageFileNameA.Call(handle, uintptr(unsafe.Pointer(&imageFileName)), uintptr(fileSize))
for _, char := range imageFileName {
if char == 0 {
break
}
fileName += string(char)
}
fileName = fileName[24:]
// EnumProcessModules
moduleHandles := make([]uintptr, 1024)
var needed int32
const handleSize = unsafe.Sizeof(moduleHandles[0])
ret, _, _ = procEnumProcessModules.Call(uintptr(handle), uintptr(unsafe.Pointer(&moduleHandles[0])), handleSize*uintptr(len(moduleHandles)), uintptr(unsafe.Pointer(&needed)))
// GetModuleFileNameExA
var finalModuleHandle uintptr
for _, moduleHandle := range moduleHandles {
if moduleHandle > 0 {
var moduleFileName [200]byte
var moduleSize uint32 = 200
var moduleName string
ret, _, _ = procGetModuleFileNameExA.Call(handle, uintptr(moduleHandle), uintptr(unsafe.Pointer(&moduleFileName)), uintptr(moduleSize))
if ret != 0 {
for _, char := range moduleFileName {
if char == 0 {
break
}
moduleName += string(char)
}
moduleName = moduleName[3:]
if moduleName == fileName {
finalModuleHandle = uintptr(moduleHandle)
break
}
}
}
}
return int64(finalModuleHandle)
}
I hope this can help someone, I spent a lot of time working on this function.

Related

Golang map put if absent?

I have a map of the format:
map[string]map[string]int
In this main map, I want to do something like putIfAbsent("key", new HashMap<>() as we have in Java. What is a clean and shorthand way to do it in Go?
You can do:
var val map[string]int
val, exists := m[key]
if !exists {
val = make(map[string]int)
m[key] = val
}
If you don't need the val in the code coming below this:
if _,exists := m[key]; !exists {
m[key]=make(map[string]int)
}
If you don't intend to use the value right away, here you go...
m := make(map[string]map[string]int)
if _, ok := m["unknown"]; !ok {
m["unknown"] = make(map[string]int)
}
Below is a suggestion for improvement:
To keep things clean and easy to understand, you can define your own types. For example, if your data is a mapping of "cities to persons to age", I would do it like this:
type Person map[string]int
type City map[string]Person
m := make(City)
if _, ok := m["Dhaka"]; !ok {
m["Dhaka"] = make(Person)
}
func main() {
var testMap map[int]interface{}
testMap = make(map[int]interface{})
var addMap map[int]string
addMap = make(map[int]string)
addMap[1] = "999"
addMap[2] = "888"
Add(testMap, 111, addMap)
for key, val := range testMap {
fmt.Println(key)
for key2, val2 := range val.(map[int]string) {
fmt.Println(key2, val2)
}
}
}
func Add(_testMap map[int]interface{}, _key int, _val map[int]string) {
_, exist := _testMap[_key] // _ -> value
if exist == false {
//addmap
_testMap[_key] = _val
} else {
//whatever wanna to do
}
}

How can I make the program automatically exit after audio played over

I'm writing a small tool, it can play audio file in the command/terminal like sox. I'm using bass.dll and Golang syscall for Windows.
Here is my code, files can downloaded from comments, only run on Windows X64.
bass.go on github gist
package main
import (
"fmt"
"syscall"
"time"
"unsafe"
)
/*
基于 [bass.dll](http://us2.un4seen.com/files/bass24.zip)
和 [Golang syscall](https://github.com/golang/go/wiki/WindowsDLLs)
实现的命令行版播放器。
*/
type BassLib struct {
libBass syscall.Handle
init uintptr
free uintptr
streamCreateFile uintptr
channelPlay uintptr
channelPause uintptr
channelStop uintptr
}
func (bass *BassLib) LoadBass(bassDllPath string) bool {
bass.libBass, _ = syscall.LoadLibrary(bassDllPath)
if bass.libBass == 0 {
fmt.Println("load library result")
fmt.Println(bass.libBass)
return false
}
bass.init, _ = syscall.GetProcAddress(bass.libBass, "BASS_Init")
// BASS_init(device, freq, flags, win, clsid)
// see http://www.un4seen.com/doc/#bass/BASS_Init.html
device := 1
syscall.Syscall6(bass.init, 5, uintptr(device), uintptr(44100), uintptr(0), uintptr(0), uintptr(0), 0)
return true
}
func StrPtr(s string) uintptr {
// return uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(s)))
p, _ := syscall.UTF16PtrFromString(s)
return uintptr(unsafe.Pointer(p))
}
func (bass *BassLib) PlayFile(filePath string) {
bass.streamCreateFile, _ = syscall.GetProcAddress(bass.libBass, "BASS_StreamCreateFile")
// hstream = BASS_StreamCreateFile(mem=0, &file, offset=0, length=0, flags=(A_IsUnicode ? 0x80000000 : 0x40000))
// see http://www.un4seen.com/doc/#bass/BASS_StreamCreateFile.html
var bassUnicode uint32 = 0x80000000
hstream, _, _ := syscall.Syscall6(bass.streamCreateFile, 5, uintptr(0), StrPtr(filePath), uintptr(0), uintptr(0), uintptr(bassUnicode), 0)
// bassErrorGetCode, _ := syscall.GetProcAddress(bass.libBass, "BASS_ErrorGetCode")
// errCode, _, _ := syscall.Syscall(uintptr(bassErrorGetCode), 0, 0, 0, 0)
// fmt.Println(errCode)
fmt.Println("hstream")
fmt.Println(hstream)
bass.channelPlay, _ = syscall.GetProcAddress(bass.libBass, "BASS_ChannelPlay")
// BASS_ChannelPlay(hstream)
// see http://www.un4seen.com/doc/#bass/BASS_ChannelPlay.html
ret, _, _ := syscall.Syscall(bass.channelPlay, 2, hstream, uintptr(0), 0)
bassErrorGetCode, _ := syscall.GetProcAddress(bass.libBass, "BASS_ErrorGetCode")
errCode, _, _ := syscall.Syscall(bassErrorGetCode, 0, 0, 0, 0)
fmt.Println(errCode)
fmt.Println(ret)
// sleep to wait playing mp3 file
time.Sleep(time.Second * 10)
// bass.channelPause, _ = syscall.GetProcAddress(bass.libBass, "BASS_ChannelPause")
// bass.channelStop, _ = syscall.GetProcAddress(bass.libBass, "BASS_ChannelStop")
// return true
}
func (bass *BassLib) UnLoad() {
if bass.libBass != 0 {
bass.free, _ = syscall.GetProcAddress(bass.libBass, "BASS_Free")
syscall.Syscall(bass.free, 0, 0, 0, 0)
// BASS_Free()
// see http://www.un4seen.com/doc/#bass/BASS_Free.html
syscall.FreeLibrary(bass.libBass)
}
}
func main() {
bass := &BassLib{}
bass.LoadBass("C:\\workspace\\play\\bass.dll")
bass.PlayFile("C:\\workspace\\play\\sample.mp3")
bass.UnLoad()
}
There is a big problem:
if time.Sleep code (bass.go line 68) not added , no sound played with quickly quit out.
When I added time.Sleep(time.Second * 10) code, maybe the audio duration more than 10 seconds.
Is there any possibility that make the program automatically exit after audio played over?
I think you can use defer keyword of golang to trigger an exit when play function have done.
You can refer here: A Tour of Go | Defer
Or here: Golang Blog | Defer
package main
import "fmt"
func main() {
defer fmt.Println("world")
fmt.Println("hello")
}
==========
$ go run main.go
hello
world
I would strongly recommend going through Effective Go on the golang.org website (it's not a long read, I am sure you can go through all the ideas in a single day), paying special attention to the concurrency section.
The whole idea behind Go is to make concurrency and asynchronous programming easy, and it uses several language constructs (channels, goroutines) especially designed to help you handle these cases.
For example, you can use a channel to signal:
func main() {
// end signal
finished := make(chan bool)
// create and run a goroutine
go func() {
// do your bass stuff here
...
// send a signal
finished <- true
}()
// wait
<-finished
}
A common pattern is also to pass the channel to the function doing the job:
func main() {
// end signal
finished := make(chan bool)
// PlayFile is responsible for
// signalling 'finished' when done
go PlayFile(someFile, finished);
// wait
<-finished
}
Or, if you have multiple routines, you will use a WaitGroup:
func main() {
// create the waitgroup
var wg sync.WaitGroup
// number of semaphores
wg.Add(1)
go func() {
// notify WaitGroup when done
// (the 'defer' keyword means
// this call will be executed before
// returning from the method)
defer wg.Done()
// do your bass stuff here
...
}()
wg.Wait()
}
Thanks everyone. Can solve the problem with BASS_ChannelGetLength and BASS_ChannelGetPosition functions.
Here is the code:
// +build windows
package main
import (
"fmt"
"syscall"
"time"
"unsafe"
)
/*
基于 [bass.dll](http://us2.un4seen.com/files/bass24.zip)
和 [Golang syscall](https://github.com/golang/go/wiki/WindowsDLLs)
实现的命令行版播放器。
*/
type (
BASSErrorGetCode int32
)
const (
BassUnicode uint32 = 0x80000000 // BASS_UNICODE
BassSampleFloat uint32 = 256 // BASS_SAMPLE_FLOAT
BassPosByte uint64 = 0 // BASS_POS_BYTE
)
type BassLib struct {
libBass syscall.Handle
init uintptr
free uintptr
streamCreateFile uintptr
channelPlay uintptr
channelPause uintptr
channelStop uintptr
channelGetLength uintptr
channelGetPosition uintptr
channelBytes2Seconds uintptr
}
func (bass *BassLib) LoadBass(bassDllPath string) bool {
bass.libBass, _ = syscall.LoadLibrary(bassDllPath)
if bass.libBass == 0 {
fmt.Println("Load `bass.dll` library failed!")
errCode := bass.GetBassErrorGetCode()
fmt.Println("Bass_Init failed!")
fmt.Println(errCode)
return false
}
bass.init, _ = syscall.GetProcAddress(bass.libBass, "BASS_Init")
// BASS_Init(device, freq, flags, win, clsid)
// see http://www.un4seen.com/doc/#bass/BASS_Init.html
device := 1
r, _, _ := syscall.Syscall6(bass.init, 5, uintptr(device), uintptr(44100), uintptr(0), uintptr(0), uintptr(0), 0)
// var ret = *(* int)(unsafe.Pointer(&r))
if r == 0 {
errCode := bass.GetBassErrorGetCode()
fmt.Println("Bass_Init failed!")
fmt.Println(errCode)
return false
}
return true
}
func StrPtr(s string) uintptr {
p, _ := syscall.UTF16PtrFromString(s)
return uintptr(unsafe.Pointer(p))
}
func (bass *BassLib) PlayFile(filePath string) {
bass.streamCreateFile, _ = syscall.GetProcAddress(bass.libBass, "BASS_StreamCreateFile")
// hStream = BASS_StreamCreateFile(mem=0, &file, offset=0, length=0, flags=(A_IsUnicode ? 0x80000000 : 0x40000))
// see http://www.un4seen.com/doc/#bass/BASS_StreamCreateFile.html
hStream, _, _ := syscall.Syscall6(bass.streamCreateFile, 5, uintptr(0), StrPtr(filePath), uintptr(0), uintptr(0), uintptr(BassUnicode|BassSampleFloat), 0)
bass.channelPlay, _ = syscall.GetProcAddress(bass.libBass, "BASS_ChannelPlay")
// BASS_ChannelPlay(hStream)
// see http://www.un4seen.com/doc/#bass/BASS_ChannelPlay.html
r, _, _ := syscall.Syscall(bass.channelPlay, 2, hStream, uintptr(0), 0)
if r == 1 {
totalDuration := bass.GetAudioByteLength(hStream)
// currentPos := bass.GetAudioCurrentBytePosition(hStream)
fmt.Println(totalDuration)
// fmt.Println(currentPos)
time.Sleep(time.Second*1)
for {
currentPos := bass.GetAudioCurrentBytePosition(hStream)
if currentPos >= totalDuration {
break
}
}
} else {
errCode := bass.GetBassErrorGetCode()
fmt.Println("Bass_ChannelPlay failed!")
fmt.Println(errCode)
}
}
func (bass *BassLib) GetBassErrorGetCode() BASSErrorGetCode {
bassErrorGetCode, _ := syscall.GetProcAddress(bass.libBass, "BASS_ErrorGetCode")
// BASS_ErrorGetCode()
// BASS_OK BASSErrorGetCode = 0 // all is OK
// BASS_ERROR_MEM BASSErrorGetCode = 1 // memory error
// ...
// see http://www.un4seen.com/doc/#bass/BASS_ErrorGetCode.html
errCode, _, _ := syscall.Syscall(bassErrorGetCode, 0, 0, 0, 0)
var iErrCode = *(*BASSErrorGetCode)(unsafe.Pointer(&errCode))
return iErrCode
}
func (bass *BassLib) GetAudioByteLength(handle uintptr) uintptr {
// (QWORD) BASS_ChannelGetLength(handle=hStream, mode)
// see http://www.un4seen.com/doc/#bass/BASS_ChannelGetLength.html
bass.channelGetLength, _ = syscall.GetProcAddress(bass.libBass, "BASS_ChannelGetLength")
len, _, _ := syscall.Syscall(bass.channelGetLength, 2, handle, uintptr(BassPosByte), 0)
return len
}
func (bass *BassLib) GetAudioCurrentBytePosition(handle uintptr) uintptr {
// BASS_ChannelGetPosition(handle=hStream, mode)
// see http://www.un4seen.com/doc/#bass/BASS_ChannelGetPosition.html
bass.channelGetPosition, _ = syscall.GetProcAddress(bass.libBass, "BASS_ChannelGetPosition")
pos, _, _ := syscall.Syscall(bass.channelGetPosition, 2, handle, uintptr(BassPosByte), 0)
return pos
}
func (bass *BassLib) GetChannelBytes2Seconds(handle uintptr, pos uintptr) uintptr {
// BASS_ChannelBytes2Seconds(handle=hStream, pos)
// see http://www.un4seen.com/doc/#bass/BASS_ChannelBytes2Seconds.html
// bass.channelBytes2Seconds, _ = syscall.GetProcAddress(bass.libBass, "BASS_ChannelBytes2Seconds")
len, _, _ := syscall.Syscall(bass.channelBytes2Seconds, 2, handle, pos, 0)
return len
}
func (bass *BassLib) UnLoad() {
if bass.libBass != 0 {
bass.free, _ = syscall.GetProcAddress(bass.libBass, "BASS_Free")
syscall.Syscall(bass.free, 0, 0, 0, 0)
// BASS_Free()
// see http://www.un4seen.com/doc/#bass/BASS_Free.html
syscall.FreeLibrary(bass.libBass)
}
}
func main() {
bass := &BassLib{}
bass.LoadBass("C:\\workspace\\play\\bass.dll")
bass.PlayFile("C:\\workspace\\play\\sample.mp3")
bass.UnLoad()
}
Also you can get at https://gist.github.com/ycrao/e7d1df181f870091b4a6d298d6ea2770#file-bass_play-go-L81-L91 .

why does Logger.Output acquire mutex before checking Lshortfile/Llongfile flag

In Go's implementation of (l *Logger) Output, why acquire mutex before if l.flag&(Lshortfile|Llongfile) != 0 {?
func (l *Logger) Output(calldepth int, s string) error {
now := time.Now() // get this early.
var file string
var line int
l.mu.Lock()
defer l.mu.Unlock()
if l.flag&(Lshortfile|Llongfile) != 0 {
// Release lock while getting caller info - it's expensive.
l.mu.Unlock()
var ok bool
_, file, line, ok = runtime.Caller(calldepth)
if !ok {
file = "???"
line = 0
}
l.mu.Lock()
}
l.buf = l.buf[:0]
l.formatHeader(&l.buf, now, file, line)
l.buf = append(l.buf, s...)
if len(s) == 0 || s[len(s)-1] != '\n' {
l.buf = append(l.buf, '\n')
}
_, err := l.out.Write(l.buf)
return err
}
what about this?
func (l *Logger) Output(calldepth int, s string) error {
now := time.Now() // get this early.
var file string
var line int
if l.flag&(Lshortfile|Llongfile) != 0 {
var ok bool
_, file, line, ok = runtime.Caller(calldepth)
if !ok {
file = "???"
line = 0
}
}
// acquire mutex here, avoid aquire mutex twice.
l.mu.Lock()
defer l.mu.Unlock()
l.buf = l.buf[:0]
l.formatHeader(&l.buf, now, file, line)
l.buf = append(l.buf, s...)
if len(s) == 0 || s[len(s)-1] != '\n' {
l.buf = append(l.buf, '\n')
}
_, err := l.out.Write(l.buf)
return err
}
Logger.flag is the field protected by a mutex, not the constants.
log.Logger has a SetFlags method that can be called at any time from any goroutine. This method is implemented as:
// SetFlags sets the output flags for the logger.
// The flag bits are Ldate, Ltime, and so on.
func (l *Logger) SetFlags(flag int) {
l.mu.Lock()
defer l.mu.Unlock()
l.flag = flag
}
The documentation itself make it clear that a logger is safe for concurrent use:
A Logger can be used simultaneously from multiple goroutines; ...
To ensure this guarantee, any field of Logger that can be modified must be synchronized. If you access l.flag without proper synchronization, you are introducing a race condition.

What are equal API for syscall.StringToUTF16, syscall.CreateFile and syscall.DeviceIoControl in mac

I have the following Go code in Windows, and I want to port it to Mac.
What are equal API for syscall.StringToUTF16, syscall.CreateFile in Mac?
import (
"syscall"
"unsafe"
"github.com/golang/glog" )
import "C"
const loggerDeviceName string = `\\.\Global\DGAPIMon`
var (
loghandle syscall.Handle
logdevid uint32 )
func InitLogger() (err error) {
// Setup the device id for the logger ioctl
deviceType := 0x00008307
access := 0
function := 0x72 // LOG STRING
method := 0
logdevid = uint32(((deviceType) << 16) | ((access) << 14) | ((function) << 2) | (method))
devPath := syscall.StringToUTF16(loggerDeviceName)
loghandle, err = syscall.CreateFile(&devPath[0], syscall.GENERIC_READ|syscall.GENERIC_WRITE,
0, nil, syscall.OPEN_EXISTING, syscall.FILE_ATTRIBUTE_NORMAL, 0)
if err == nil {
glog.V(3).Infof("loghandle: %v logdevid: 0x%x", loghandle, logdevid)
}
return err }
loghandle and logdevid are using as below to log the message to a file.
typedef struct tagLOGSTRING
{
unsigned long level;
wchar_t message[1024];
}LOGSTRING;
func LogMsg(b []byte) (err error) {
var logstring C.LOGSTRING
logstring.level = 0
tmp := syscall.StringToUTF16(string(b[:]))
for i := range tmp {
logstring.message[i] = C.uint16_t(tmp[i])
}
err = syscall.DeviceIoControl(loghandle, logdevid,
(*byte)(unsafe.Pointer(&logstring)),
(uint32)(unsafe.Sizeof(logstring)),
nil, 0, nil, nil)
return err
}

Scheme interpreter in Go

I'm quite a basic Go programmer and I've been taking a look at this small Scheme interpreter and I've been trying to understand how it works.
I found it here:
https://pkelchte.wordpress.com/2013/12/31/scm-go/
I read the webpage, but I'm still struggling to understand how it works because the source code is obviously written by someone who's a lot more familiar with Go than I am.
Particularly there's these lines that I'm struggling to understand:
e := expression.(type) // Line 73
I'm not sure what the .(type) part means, I thought it was casting but it doesn't look like the casting I've seen before.
switch p := procedure.(type) {
case func(...scmer) scmer:
value = p(args...)
case proc:
en := &env{make(vars), p.en}
switch params := p.params.(type) {
case []scmer:
for i, param := range params {
en.vars[param.(symbol)] = args[i]
}
default:
en.vars[params.(symbol)] = args
}
value = eval(p.body, en)
I don't really understand any of this code to be honest. Lines 73 - 86
*tokens = (*tokens)[1:] // Line 208
I'm not sure what this line means due to it's weird syntax. I get that its pointers and that the parenthesises are because of the *. But I'm not sure what that lines doing.
Finally there's these lines:
token := (*tokens)[0]
*tokens = (*tokens)[1:]
switch token {
case "(": //a list begins
L := make([]scmer, 0)
for (*tokens)[0] != ")" {
if i := readFrom(tokens); i != symbol("") {
L = append(L, i)
}
}
*tokens = (*tokens)[1:]
return L
I don't know what these lines do either. Lines 198 - 209
Here's the complete code if you want it, I realise it's 250 lines long but I'd really appreciate as many explanations about what its doing as possible.
/*
* A minimal Scheme interpreter, as seen in lis.py and SICP
* http://norvig.com/lispy.html
* http://mitpress.mit.edu/sicp/full-text/sicp/book/node77.html
*
* Pieter Kelchtermans 2013
* LICENSE: WTFPL 2.0
*/
package main
import (
"bufio"
"fmt"
"log"
"os"
"reflect"
"strconv"
"strings"
)
func main() {
Repl()
}
/*
Eval / Apply
*/
func eval(expression scmer, en *env) (value scmer) {
switch e := expression.(type) {
case number:
value = e
case symbol:
value = en.Find(e).vars[e]
case []scmer:
switch car, _ := e[0].(symbol); car {
case "quote":
value = e[1]
case "if":
if eval(e[1], en).(bool) {
value = eval(e[2], en)
} else {
value = eval(e[3], en)
}
case "set!":
v := e[1].(symbol)
en.Find(v).vars[v] = eval(e[2], en)
value = "ok"
case "define":
en.vars[e[1].(symbol)] = eval(e[2], en)
value = "ok"
case "lambda":
value = proc{e[1], e[2], en}
case "begin":
for _, i := range e[1:] {
value = eval(i, en)
}
default:
operands := e[1:]
values := make([]scmer, len(operands))
for i, x := range operands {
values[i] = eval(x, en)
}
value = apply(eval(e[0], en), values)
}
default:
log.Println("Unknown expression type - EVAL", e)
}
return
}
func apply(procedure scmer, args []scmer) (value scmer) {
switch p := procedure.(type) {
case func(...scmer) scmer:
value = p(args...)
case proc:
en := &env{make(vars), p.en}
switch params := p.params.(type) {
case []scmer:
for i, param := range params {
en.vars[param.(symbol)] = args[i]
}
default:
en.vars[params.(symbol)] = args
}
value = eval(p.body, en)
default:
log.Println("Unknown procedure type - APPLY", p)
}
return
}
type proc struct {
params, body scmer
en *env
}
/*
Environments
*/
type vars map[symbol]scmer
type env struct {
vars
outer *env
}
func (e *env) Find(s symbol) *env {
if _, ok := e.vars[s]; ok {
return e
} else {
return e.outer.Find(s)
}
}
/*
Primitives
*/
var globalenv env
func init() {
globalenv = env{
vars{ //aka an incomplete set of compiled-in functions
"+": func(a ...scmer) scmer {
v := a[0].(number)
for _, i := range a[1:] {
v += i.(number)
}
return v
},
"-": func(a ...scmer) scmer {
v := a[0].(number)
for _, i := range a[1:] {
v -= i.(number)
}
return v
},
"*": func(a ...scmer) scmer {
v := a[0].(number)
for _, i := range a[1:] {
v *= i.(number)
}
return v
},
"/": func(a ...scmer) scmer {
v := a[0].(number)
for _, i := range a[1:] {
v /= i.(number)
}
return v
},
"<=": func(a ...scmer) scmer {
return a[0].(number) <= a[1].(number)
},
"equal?": func(a ...scmer) scmer {
return reflect.DeepEqual(a[0], a[1])
},
"cons": func(a ...scmer) scmer {
switch car := a[0]; cdr := a[1].(type) {
case []scmer:
return append([]scmer{car}, cdr...)
default:
return []scmer{car, cdr}
}
},
"car": func(a ...scmer) scmer {
return a[0].([]scmer)[0]
},
"cdr": func(a ...scmer) scmer {
return a[0].([]scmer)[1:]
},
"list": eval(read(
"(lambda z z)"),
&globalenv),
},
nil}
}
/*
Parsing
*/
//symbols, numbers, expressions, procedures, lists, ... all implement this interface, which enables passing them along in the interpreter
type scmer interface{}
type symbol string //symbols are represented by strings
type number float64 //numbers by float64
func read(s string) (expression scmer) {
tokens := tokenize(s)
return readFrom(&tokens)
}
//Syntactic Analysis
func readFrom(tokens *[]string) (expression scmer) {
//pop first element from tokens
token := (*tokens)[0]
*tokens = (*tokens)[1:]
switch token {
case "(": //a list begins
L := make([]scmer, 0)
for (*tokens)[0] != ")" {
if i := readFrom(tokens); i != symbol("") {
L = append(L, i)
}
}
*tokens = (*tokens)[1:]
return L
default: //an atom occurs
if f, err := strconv.ParseFloat(token, 64); err == nil {
return number(f)
} else {
return symbol(token)
}
}
}
//Lexical Analysis
func tokenize(s string) []string {
return strings.Split(
strings.Replace(strings.Replace(s, "(", "( ",
-1), ")", " )",
-1), " ")
}
/*
Interactivity
*/
func String(v scmer) string {
switch v := v.(type) {
case []scmer:
l := make([]string, len(v))
for i, x := range v {
l[i] = String(x)
}
return "(" + strings.Join(l, " ") + ")"
default:
return fmt.Sprint(v)
}
}
func Repl() {
scanner := bufio.NewScanner(os.Stdin)
for fmt.Print("> "); scanner.Scan(); fmt.Print("> ") {
fmt.Println("==>", String(eval(read(scanner.Text()), &globalenv)))
}
}
First one is type switch. You can read about it more here.
Second one is slicing like in python (if you know it, should be familiar)
ls = ls[1:]
We're skipping first item and taking the rest of the slice/list.
As I see from your comments, I think it's not a good starting point to experiment with golang.
e := expression.(type) is a type assertion.
switch p := procedure.(type) { (with the following case statements) is a type switch.
*tokens = (*tokens)[1:] is a slice expression changing the value stored in *tokens.
token := (*tokens)[0] // Set token to first element of tokens slice.
*tokens = (*tokens)[1:] // Update tokens to remove first element.
switch token { // Switch on token.
case "(": //a list begins // If token is "("
L := make([]scmer, 0) // Make a new, empty slice.
for (*tokens)[0] != ")" { // While the first element of tokens is not ")":
// Read from tokens; if not empty symbol:
if i := readFrom(tokens); i != symbol("") {
L = append(L, i) // Add the token read (in i) to L.
}
}
*tokens = (*tokens)[1:] // Remove the first element from tokens.
return L // Return the newly created slice.

Resources