on master help sign in
tcl from file (and tests)
commit9f06673fc33a6c0560cbf76ab569438ed20ea170 authormason clayton <masongclayton@gmail.com> dateTue 26 Feb 2019, 15:45 (8 years ago) parent75fb729
13 files changed+345−70
Dockerfile+3−2 README.md+2−2 languages/tcl/main.go+58−19 languages/tcl/pry_tcl.c−39 languages/tcl/pry_tcl.h−8 languages/tcl/tcl.pc+15 test_files/hello_tcl.tcl+3 tests/tcl/1_plus_1.exp+28 tests/tcl/a_few_outputs.exp+89 tests/tcl/from_file.exp+38 tests/tcl/input_output.exp+78 tests/tcl/status_line.exp+6 tests/tcl/unknown_command.exp+25
Dockerfile+3−2
@@ -36,7 +36,7 @@ ENV GOPATH=/gocode
3636ADD . /gocode/src/github.com/replit/prybar
3737WORKDIR /gocode/src/github.com/replit/prybar
3838
39−RUN which node
39+RUN cp languages/tcl/tcl.pc /usr/lib/pkgconfig/
4040
4141RUN make \
4242 prybar-python2 \
@@ -45,7 +45,8 @@ RUN make \
4545 prybar-lua \
4646 prybar-spidermonkey \
4747 prybar-nodejs \
48− prybar-julia
48+ prybar-julia \
49+ prybar-tcl
4950
5051ENV LC_ALL=C.UTF-8
5152
README.md+2−2
@@ -37,9 +37,9 @@ Usage of ./prybar-language:
3737| Python 3.x | ✔ | ✔ | ✔ | ✔ | ✔ | ✔ |
3838| Ruby 2.5 | ✔ | ✔ | ✔ | ✔ | ✗ | ✗ |
3939| Lua 5.1 | ✔ | ✗ | ✔ | ✔ | ✗ | ✔ |
40−| Tcl | ✔ | ✔ | ✗ | ✗ | ✗ | ✗ |
40+| Tcl | ✔ | ✔ | ✔ | ✗ | ✗ | - |
4141| R | ✔ | ✗ | ✗ | ✔ | ✗ | ✗ |
42−| Javascript (spidermonkey) | ✔ | ✗ | ✗ | ✗ | ✗ | ✗ |
42+| Javascript (spidermonkey) | ✔ | ✗ | ✗ | ✗ | ✗ | - |
4343| Javascript (nodejs) | ✔ | ✔ | ✔ | ✔ | ✔ | ✔ |
4444| Julia | ✔ | ✗ | ✔ | ✔ | ✗ | ✔ |
4545
languages/tcl/main.go+58−19
@@ -4,42 +4,81 @@ package main
44
55/*
66#cgo pkg-config: tcl
7−#include "pry_tcl.h"
7+#include <tcl.h>
8+#include <stdlib.h>
89
910*/
1011import "C"
1112
1213import (
14+ "fmt"
15+ "os"
16+ "strconv"
1317 "unsafe"
1418)
1519
16−type Tcl struct{}
20+type Tcl struct {
21+ interp *C.Tcl_Interp
22+}
23+
24+func (p *Tcl) Open() {
25+ p.interp = C.Tcl_CreateInterp()
1726
18−func (p Tcl) Open() {
19− C.pry_open()
27+ if C.Tcl_Init(p.interp) != C.TCL_OK {
28+ panic("tcl interp did not init")
29+ }
2030}
2131
22−func (p Tcl) Version() string {
23− cver := C.pry_version()
24− ver := C.GoString(cver)
25− C.free(unsafe.Pointer(cver))
26− return "TCL " + ver
32+func (p *Tcl) Version() string {
33+ major := C.int(0)
34+ minor := C.int(0)
35+ patch := C.int(0)
36+
37+ C.Tcl_GetVersion(
38+ &major,
39+ &minor,
40+ &patch,
41+ nil,
42+ )
43+
44+ return "TCL " +
45+ strconv.Itoa(int(major)) + "." +
46+ strconv.Itoa(int(minor)) + "." +
47+ strconv.Itoa(int(patch))
2748}
2849
29−func (p Tcl) Eval(code string) {
50+func (p *Tcl) eval(code string) string {
3051 ccode := C.CString(code)
31− defer C.free(unsafe.Pointer(ccode))
32− C.pry_eval(ccode)
52+
53+ status := C.Tcl_Eval(p.interp, ccode)
54+
55+ result := C.GoString(C.Tcl_GetStringResult(p.interp))
56+
57+ if status == C.TCL_OK {
58+ return result
59+ }
60+
61+ errstr := C.GoString(C.Tcl_GetStringResult(p.interp))
62+ fmt.Fprintf(os.Stderr, "error: %s\n", errstr)
63+ return ""
3364}
3465
35−func (p Tcl) EvalExpression(code string) string {
36− ccode := C.CString(code)
37− defer C.free(unsafe.Pointer(ccode))
38− return C.GoString(C.pry_eval(ccode))
66+func (p *Tcl) EvalFile(file string, args []string) {
67+ cfile := C.CString(file)
68+ defer C.free(unsafe.Pointer(cfile))
69+
70+ C.Tcl_EvalFile(p.interp, cfile)
3971}
4072
41−func (p Tcl) Close() {
42− C.pry_close()
73+func (p *Tcl) Eval(code string) {
74+ p.eval(code)
4375}
4476
45−var Instance = Tcl{}
77+func (p *Tcl) EvalExpression(code string) string {
78+ return p.eval(code)
79+}
80+
81+func (p *Tcl) Close() {
82+ C.Tcl_Finalize()
83+}
84+var Instance = &Tcl{}
languages/tcl/pry_tcl.cdeleted−39
@@ -1,39 +0,0 @@
1−#include "pry_tcl.h"
2−
3−Tcl_Interp *interp;
4−
5−void pry_open()
6−{
7− interp = Tcl_CreateInterp();
8− if (Tcl_Init(interp) != TCL_OK)
9− {
10− exit(8);
11− }
12−}
13−
14−void pry_close()
15−{
16− Tcl_Finalize();
17−}
18−
19−char *pry_version()
20−{
21− char *result = malloc(100);
22− int a, b, c;
23− Tcl_GetVersion(&a, &b, &c, NULL);
24− sprintf(result, "%d.%d.%d", a, b, c);
25− return result;
26−}
27−
28−char *pry_eval(const char *code)
29−{
30− if (Tcl_Eval(interp, code) == TCL_OK)
31− {
32− return Tcl_GetStringResult(interp);
33− }
34− else
35− {
36− fprintf(stderr, "error: %s\n", Tcl_GetStringResult(interp));
37− return "";
38− }
39−}
languages/tcl/pry_tcl.hdeleted−8
@@ -1,8 +0,0 @@
1−#include <stdlib.h>
2−#include <tcl.h>
3−
4−
5−void pry_open();
6−void pry_close();
7−char *pry_version();
8−char *pry_eval(const char *code);
languages/tcl/tcl.pcnew file+15
@@ -0,0 +1,15 @@
1+# tcl pkg-config source file
2+
3+prefix=/usr
4+exec_prefix=/usr
5+libdir=/usr/lib
6+includedir=${prefix}/include
7+
8+Name: Tool Command Language
9+Description: Tcl is a powerful, easy-to-learn dynamic programming language, suitable for a wide range of uses.
10+URL: http://www.tcl.tk/
11+Version: 8.6.9
12+Requires.private: zlib >= 1.2.3
13+Libs: -L${libdir} -ltcl8.6 -ltclstub8.6
14+Libs.private: -ldl -lz -lpthread -lm
15+Cflags: -I${includedir} -I${includedir}/tcl
test_files/hello_tcl.tclnew file+3
@@ -0,0 +1,3 @@
1+puts "Hello, Tcl!"
2+
3+set my_num 1000
tests/tcl/1_plus_1.expnew file+28
@@ -0,0 +1,28 @@
1+#!/usr/bin/expect -f
2+
3+set timeout -1
4+spawn ./prybar-tcl -q -i
5+match_max 100000
6+expect -exact "--> "
7+send -- "e"
8+expect -exact "\r--> e\[0K\r\[5C"
9+send -- "x"
10+expect -exact "\r--> ex\[0K\r\[6C"
11+send -- "p"
12+expect -exact "\r--> exp\[0K\r\[7C"
13+send -- "r"
14+expect -exact "\r--> expr\[0K\r\[8C"
15+send -- " "
16+expect -exact "\r--> expr \[0K\r\[9C"
17+send -- "1"
18+expect -exact "\r--> expr 1\[0K\r\[10C"
19+send -- "+"
20+expect -exact "\r--> expr 1+\[0K\r\[11C"
21+send -- "1"
22+expect -exact "\r--> expr 1+1\[0K\r\[12C"
23+send -- "\r"
24+expect -exact "\r--> expr 1+1\[0K\r\[12C\r
25+2\r
26+--> "
27+send -- ""
28+expect eof
tests/tcl/a_few_outputs.expnew file+89
@@ -0,0 +1,89 @@
1+#!/usr/bin/expect -f
2+
3+set timeout -1
4+spawn ./prybar-tcl -q -i
5+match_max 100000
6+expect -exact "--> "
7+send -- "p"
8+expect -exact "\r--> p\[0K\r\[5C"
9+send -- "u"
10+expect -exact "\r--> pu\[0K\r\[6C"
11+send -- "t"
12+expect -exact "\r--> put\[0K\r\[7C"
13+send -- "s"
14+expect -exact "\r--> puts\[0K\r\[8C"
15+send -- " "
16+expect -exact "\r--> puts \[0K\r\[9C"
17+send -- "\""
18+expect -exact "\r--> puts \"\[0K\r\[10C"
19+send -- "h"
20+expect -exact "\r--> puts \"h\[0K\r\[11C"
21+send -- "e"
22+expect -exact "\r--> puts \"he\[0K\r\[12C"
23+send -- "y"
24+expect -exact "\r--> puts \"hey\[0K\r\[13C"
25+send -- "\""
26+expect -exact "\r--> puts \"hey\"\[0K\r\[14C"
27+send -- "\r"
28+expect -exact "\r--> puts \"hey\"\[0K\r\[14C\r
29+hey\r\r
30+\r
31+--> "
32+send -- "p"
33+expect -exact "\r--> p\[0K\r\[5C"
34+send -- "u"
35+expect -exact "\r--> pu\[0K\r\[6C"
36+send -- "t"
37+expect -exact "\r--> put\[0K\r\[7C"
38+send -- "s"
39+expect -exact "\r--> puts\[0K\r\[8C"
40+send -- " "
41+expect -exact "\r--> puts \[0K\r\[9C"
42+send -- "\""
43+expect -exact "\r--> puts \"\[0K\r\[10C"
44+send -- "h"
45+expect -exact "\r--> puts \"h\[0K\r\[11C"
46+send -- "e"
47+expect -exact "\r--> puts \"he\[0K\r\[12C"
48+send -- "y"
49+expect -exact "\r--> puts \"hey\[0K\r\[13C"
50+send -- "\""
51+expect -exact "\r--> puts \"hey\"\[0K\r\[14C"
52+send -- "\r"
53+expect -exact "\r--> puts \"hey\"\[0K\r\[14C\r
54+hey\r\r
55+\r
56+--> "
57+send -- "p"
58+expect -exact "\r--> p\[0K\r\[5C"
59+send -- "u"
60+expect -exact "\r--> pu\[0K\r\[6C"
61+send -- "t"
62+expect -exact "\r--> put\[0K\r\[7C"
63+send -- "s"
64+expect -exact "\r--> puts\[0K\r\[8C"
65+send -- " "
66+expect -exact "\r--> puts \[0K\r\[9C"
67+send -- "h"
68+expect -exact "\r--> puts h\[0K\r\[10C"
69+send -- "e"
70+expect -exact "\r--> puts he\[0K\r\[11C"
71+send -- "e"
72+expect -exact "\r--> puts hee\[0K\r\[12C"
73+send -- "e"
74+expect -exact "\r--> puts heee\[0K\r\[13C"
75+send -- "e"
76+expect -exact "\r--> puts heeee\[0K\r\[14C"
77+send -- "e"
78+expect -exact "\r--> puts heeeee\[0K\r\[15C"
79+send -- "e"
80+expect -exact "\r--> puts heeeeee\[0K\r\[16C"
81+send -- "y"
82+expect -exact "\r--> puts heeeeeey\[0K\r\[17C"
83+send -- "\r"
84+expect -exact "\r--> puts heeeeeey\[0K\r\[17C\r
85+heeeeeey\r\r
86+\r
87+--> "
88+send -- ""
89+expect eof
tests/tcl/from_file.expnew file+38
@@ -0,0 +1,38 @@
1+#!/usr/bin/expect -f
2+
3+set timeout -1
4+spawn ./prybar-tcl -q -i ./test_files/hello_tcl.tcl
5+match_max 100000
6+expect -exact "Hello, Tcl!\r\r
7+--> "
8+send -- "p"
9+expect -exact "\r--> p\[0K\r\[5C"
10+send -- "u"
11+expect -exact "\r--> pu\[0K\r\[6C"
12+send -- "t"
13+expect -exact "\r--> put\[0K\r\[7C"
14+send -- "s"
15+expect -exact "\r--> puts\[0K\r\[8C"
16+send -- " "
17+expect -exact "\r--> puts \[0K\r\[9C"
18+send -- "\$"
19+expect -exact "\r--> puts \$\[0K\r\[10C"
20+send -- "m"
21+expect -exact "\r--> puts \$m\[0K\r\[11C"
22+send -- "y"
23+expect -exact "\r--> puts \$my\[0K\r\[12C"
24+send -- "_"
25+expect -exact "\r--> puts \$my_\[0K\r\[13C"
26+send -- "n"
27+expect -exact "\r--> puts \$my_n\[0K\r\[14C"
28+send -- "u"
29+expect -exact "\r--> puts \$my_nu\[0K\r\[15C"
30+send -- "m"
31+expect -exact "\r--> puts \$my_num\[0K\r\[16C"
32+send -- "\r"
33+expect -exact "\r--> puts \$my_num\[0K\r\[16C\r
34+1000\r\r
35+\r
36+--> "
37+send -- ""
38+expect eof
tests/tcl/input_output.expnew file+78
@@ -0,0 +1,78 @@
1+#!/usr/bin/expect -f
2+
3+set timeout -1
4+spawn ./prybar-tcl -q -i
5+match_max 100000
6+expect -exact "--> "
7+send -- "s"
8+expect -exact "\r--> s\[0K\r\[5C"
9+send -- "e"
10+expect -exact "\r--> se\[0K\r\[6C"
11+send -- "t"
12+expect -exact "\r--> set\[0K\r\[7C"
13+send -- " "
14+expect -exact "\r--> set \[0K\r\[8C"
15+send -- "i"
16+expect -exact "\r--> set i\[0K\r\[9C"
17+send -- "n"
18+expect -exact "\r--> set in\[0K\r\[10C"
19+send -- "p"
20+expect -exact "\r--> set inp\[0K\r\[11C"
21+send -- " "
22+expect -exact "\r--> set inp \[0K\r\[12C"
23+send -- "\["
24+expect -exact "\r--> set inp \[\[0K\r\[13C"
25+send -- "g"
26+expect -exact "\r--> set inp \[g\[0K\r\[14C"
27+send -- "e"
28+expect -exact "\r--> set inp \[ge\[0K\r\[15C"
29+send -- "t"
30+expect -exact "\r--> set inp \[get\[0K\r\[16C"
31+send -- "s"
32+expect -exact "\r--> set inp \[gets\[0K\r\[17C"
33+send -- " "
34+expect -exact "\r--> set inp \[gets \[0K\r\[18C"
35+send -- "s"
36+expect -exact "\r--> set inp \[gets s\[0K\r\[19C"
37+send -- "t"
38+expect -exact "\r--> set inp \[gets st\[0K\r\[20C"
39+send -- "d"
40+expect -exact "\r--> set inp \[gets std\[0K\r\[21C"
41+send -- "i"
42+expect -exact "\r--> set inp \[gets stdi\[0K\r\[22C"
43+send -- "n"
44+expect -exact "\r--> set inp \[gets stdin\[0K\r\[23C"
45+send -- "\]"
46+expect -exact "\r--> set inp \[gets stdin\]\[0K\r\[24C"
47+send -- "\r"
48+expect -exact "\r--> set inp \[gets stdin\]\[0K\r\[24C\r
49+"
50+send -- "hellooooooo\r"
51+expect -exact "hellooooooo\r
52+hellooooooo\r
53+--> "
54+send -- "p"
55+expect -exact "\r--> p\[0K\r\[5C"
56+send -- "u"
57+expect -exact "\r--> pu\[0K\r\[6C"
58+send -- "t"
59+expect -exact "\r--> put\[0K\r\[7C"
60+send -- "s"
61+expect -exact "\r--> puts\[0K\r\[8C"
62+send -- " "
63+expect -exact "\r--> puts \[0K\r\[9C"
64+send -- "\$"
65+expect -exact "\r--> puts \$\[0K\r\[10C"
66+send -- "i"
67+expect -exact "\r--> puts \$i\[0K\r\[11C"
68+send -- "n"
69+expect -exact "\r--> puts \$in\[0K\r\[12C"
70+send -- "p"
71+expect -exact "\r--> puts \$inp\[0K\r\[13C"
72+send -- "\r"
73+expect -exact "\r--> puts \$inp\[0K\r\[13C\r
74+hellooooooo\r\r
75+\r
76+--> "
77+send -- ""
78+expect eof
tests/tcl/status_line.expnew file+6
@@ -0,0 +1,6 @@
1+#!/usr/bin/expect -f
2+
3+set timeout -1
4+spawn ./prybar-tcl
5+match_max 100000
6+expect eof
tests/tcl/unknown_command.expnew file+25
@@ -0,0 +1,25 @@
1+#!/usr/bin/expect -f
2+
3+set timeout -1
4+spawn ./prybar-tcl -q -i
5+match_max 100000
6+expect -exact "--> "
7+send -- "h"
8+expect -exact "\r--> h\[0K\r\[5C"
9+send -- "m"
10+expect -exact "\r--> hm\[0K\r\[6C"
11+send -- "m"
12+expect -exact "\r--> hmm\[0K\r\[7C"
13+send -- "m"
14+expect -exact "\r--> hmmm\[0K\r\[8C"
15+send -- "m"
16+expect -exact "\r--> hmmmm\[0K\r\[9C"
17+send -- "m"
18+expect -exact "\r--> hmmmmm\[0K\r\[10C"
19+send -- "\r"
20+expect -exact "\r--> hmmmmm\[0K\r\[10C\r
21+error: invalid command name \"hmmmmm\"\r
22+\r
23+--> "
24+send -- ""
25+expect eof