Skip to content

Commit 48cbc2f

Browse files
committed
Impl prototype scanner.
1 parent c76b6e3 commit 48cbc2f

File tree

10 files changed

+964
-2
lines changed

10 files changed

+964
-2
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*_string.go

README.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1-
# ringo
2-
Ruby in Go
1+
# Ringo
2+
3+
Ringo is experimental project to implement ruby interpreter in Golang.

debug/debug.go

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
Package debug implements utility functions to output debug message.
3+
4+
The debug messages can enable by Enable variable, for instance:
5+
6+
debug.Enable = true
7+
8+
debug.Print("debug message")
9+
// Output:
10+
// debug message
11+
12+
*/
13+
package debug
14+
15+
import (
16+
"fmt"
17+
"os"
18+
"path"
19+
"runtime"
20+
)
21+
22+
// Enable specifies whether the debug prints messages into the Output.
23+
var Enable = false
24+
25+
// Output specifies the destination to print out messages.
26+
var Output = os.Stderr
27+
28+
func output(s string) {
29+
_, file, line, _ := runtime.Caller(2)
30+
fmt.Fprintf(Output, "%s:%d> %s", path.Base(file), line, s)
31+
if s[len(s)-1] != '\n' {
32+
Output.Write([]byte{'\n'})
33+
}
34+
}
35+
36+
// Print prints debug message in manner of fmt.Sprint.
37+
func Print(v ...interface{}) {
38+
if Enable {
39+
output(fmt.Sprint(v...))
40+
}
41+
}
42+
43+
// Println prints debug message in manner of fmt.Sprintln.
44+
func Println(v ...interface{}) {
45+
if Enable {
46+
output(fmt.Sprintln(v...))
47+
}
48+
}
49+
50+
// Printf prints debug message in manner of fmt.Sprintf.
51+
func Printf(format string, v ...interface{}) {
52+
if Enable {
53+
output(fmt.Sprintf(format, v...))
54+
}
55+
}

debug/debug_test.go

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package debug_test
2+
3+
import (
4+
"os"
5+
6+
"github.com/harukasan/ringo/debug"
7+
)
8+
9+
func ExamplePrint() {
10+
debug.Enable = true
11+
// set output to stdout for testing
12+
debug.Output = os.Stdout
13+
//
14+
//
15+
debug.Print("Hello, world!") // line: 15
16+
// Output:
17+
// debug_test.go:15> Hello, world!
18+
}
19+
20+
func ExamplePrintln() {
21+
debug.Enable = true
22+
// set output to stdout for testing
23+
debug.Output = os.Stdout
24+
25+
debug.Println("Hello, world!") // line: 25
26+
// Output:
27+
// debug_test.go:25> Hello, world!
28+
}
29+
30+
func ExamplePrintf() {
31+
debug.Enable = true
32+
// set output to stdout for testing
33+
debug.Output = os.Stdout
34+
35+
debug.Printf("a: %v", true) // line: 35
36+
// Output:
37+
// debug_test.go:35> a: true
38+
}

scanner/error.go

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package scanner
2+
3+
import "fmt"
4+
5+
// ScanError holds an error which is caused by scanner.
6+
type ScanError struct {
7+
Pos int
8+
Err error
9+
}
10+
11+
func (e *ScanError) Error() string {
12+
return fmt.Sprintf("%v at pos=%d", e.Err, e.Pos)
13+
}

0 commit comments

Comments
 (0)