Skip to content

Commit

Permalink
introduce export/unexport by capitalize
Browse files Browse the repository at this point in the history
  • Loading branch information
hidetatz committed Sep 9, 2023
1 parent 927ff5c commit 77ebdb7
Show file tree
Hide file tree
Showing 24 changed files with 104 additions and 68 deletions.
8 changes: 4 additions & 4 deletions e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -580,20 +580,20 @@ func TestOutput(t *testing.T) {
a = 1
print(import1_2.b(a))
print(import1_2.B(a))
`),
additionalfiles: map[string]string{
"import1_2": d(`
import import1_3
a = 2
def b(x) {
return import1_3.a + a + x
def B(x) {
return import1_3.A + a + x
}
`),
"import1_3": d(`
a = 3
A = 3
`),
},
out: d(`
Expand Down
4 changes: 2 additions & 2 deletions gostd.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ func (g *gostdmodules) objs(modname string) ([]*gostdmodobj, bool) {
var gostdmods = &gostdmodules{mods: map[string][]*gostdmodobj{
"math": {
{
"pi",
"Pi",
&obj{
typ: tF64,
fval: math.Pi,
},
},
{
"add",
"Add",
&obj{
typ: tGoStdModFunc,
gostdmodfunc: func(objs ...*obj) (*obj, error) {
Expand Down
32 changes: 32 additions & 0 deletions node.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"fmt"
"unicode"
)

type assignOp int
Expand Down Expand Up @@ -142,6 +143,7 @@ const (

type node interface {
token() *token
isexported() bool
fmt.Stringer
}

Expand All @@ -150,6 +152,7 @@ type ndEof struct {
}

func (n *ndEof) token() *token { return n.tok }
func (n *ndEof) isexported() bool { return false }
func (n *ndEof) String() string {
return "ndEof{}"
}
Expand All @@ -160,6 +163,7 @@ type ndComment struct {
}

func (n *ndComment) token() *token { return n.tok }
func (n *ndComment) isexported() bool { return false }
func (n *ndComment) String() string {
return fmt.Sprintf("ndComment{message: %s}", n.message)
}
Expand All @@ -172,6 +176,7 @@ type ndAssign struct {
}

func (n *ndAssign) token() *token { return n.tok }
func (n *ndAssign) isexported() bool { return false }
func (n *ndAssign) String() string {
return fmt.Sprintf("ndAssign{left: %s, op: %s, right: %s}", nodesToStr(n.left), n.op, nodesToStr(n.right))
}
Expand All @@ -184,6 +189,7 @@ type ndIf struct {
}

func (n *ndIf) token() *token { return n.tok }
func (n *ndIf) isexported() bool { return false }
func (n *ndIf) String() string {
bs := "["
for _, block := range n.blocks {
Expand All @@ -204,6 +210,7 @@ type ndLoop struct {
}

func (n *ndLoop) token() *token { return n.tok }
func (n *ndLoop) isexported() bool { return false }
func (n *ndLoop) String() string {
return fmt.Sprintf("ndLoop{target: %s, cnt: %s, elem: %s, blocks: %s}", n.target, n.cnt, n.elem, nodesToStr(n.blocks))
}
Expand All @@ -215,6 +222,7 @@ type ndCondLoop struct {
}

func (n *ndCondLoop) token() *token { return n.tok }
func (n *ndCondLoop) isexported() bool { return false }
func (n *ndCondLoop) String() string {
return fmt.Sprintf("ndCondLoop{cond: %s, blocks: %s}", n.cond, nodesToStr(n.blocks))
}
Expand All @@ -227,6 +235,7 @@ type ndFunDef struct {
}

func (n *ndFunDef) token() *token { return n.tok }
func (n *ndFunDef) isexported() bool { return isexported(n.name) }
func (n *ndFunDef) String() string {
return fmt.Sprintf("ndFunDef{name: %s, params: %s, blocks: %s}", n.name, nodesToStr(n.params), nodesToStr(n.blocks))
}
Expand All @@ -239,6 +248,7 @@ type ndBinaryOp struct {
}

func (n *ndBinaryOp) token() *token { return n.tok }
func (n *ndBinaryOp) isexported() bool { return false }
func (n *ndBinaryOp) String() string {
return fmt.Sprintf("ndBinaryOp{left: %s, op: %s, right: %s}", n.left, n.op, n.right)
}
Expand All @@ -250,6 +260,7 @@ type ndUnaryOp struct {
}

func (n *ndUnaryOp) token() *token { return n.tok }
func (n *ndUnaryOp) isexported() bool { return false }
func (n *ndUnaryOp) String() string {
return fmt.Sprintf("ndUnaryOp{op: %s, target: %s}", n.op, n.target)
}
Expand All @@ -261,6 +272,7 @@ type ndSelector struct {
}

func (n *ndSelector) token() *token { return n.tok }
func (n *ndSelector) isexported() bool { return n.target.isexported() }
func (n *ndSelector) String() string {
return fmt.Sprintf("ndSelector{selector: %s, target: %s}", n.selector, n.target)
}
Expand All @@ -272,6 +284,7 @@ type ndIndex struct {
}

func (n *ndIndex) token() *token { return n.tok }
func (n *ndIndex) isexported() bool { return n.target.isexported() }
func (n *ndIndex) String() string {
return fmt.Sprintf("ndIndex{idx: %s, target: %s}", n.idx, n.target)
}
Expand All @@ -284,6 +297,7 @@ type ndSlice struct {
}

func (n *ndSlice) token() *token { return n.tok }
func (n *ndSlice) isexported() bool { return n.target.isexported() }
func (n *ndSlice) String() string {
return fmt.Sprintf("ndSlice{start: %s, end: %s, target: %s}", n.start, n.end, n.target)
}
Expand All @@ -295,6 +309,7 @@ type ndFuncall struct {
}

func (n *ndFuncall) token() *token { return n.tok }
func (n *ndFuncall) isexported() bool { return n.fn.isexported() }
func (n *ndFuncall) String() string {
return fmt.Sprintf("ndFuncall{fn: %s, args: %s}", n.fn, nodesToStr(n.args))
}
Expand All @@ -305,6 +320,7 @@ type ndIdent struct {
}

func (n *ndIdent) token() *token { return n.tok }
func (n *ndIdent) isexported() bool { return isexported(n.ident) }
func (n *ndIdent) String() string {
return fmt.Sprintf("ndIdent{ident: %s}", n.ident)
}
Expand All @@ -315,6 +331,7 @@ type ndStr struct {
}

func (n *ndStr) token() *token { return n.tok }
func (n *ndStr) isexported() bool { return true }
func (n *ndStr) String() string {
return fmt.Sprintf("ndStr{val: %s}", n.val)
}
Expand All @@ -325,6 +342,7 @@ type ndI64 struct {
}

func (n *ndI64) token() *token { return n.tok }
func (n *ndI64) isexported() bool { return true }
func (n *ndI64) String() string {
return fmt.Sprintf("ndI64{val: %d}", n.val)
}
Expand All @@ -335,6 +353,7 @@ type ndF64 struct {
}

func (n *ndF64) token() *token { return n.tok }
func (n *ndF64) isexported() bool { return true }
func (n *ndF64) String() string {
return fmt.Sprintf("ndF64{val: %f}", n.val)
}
Expand All @@ -345,6 +364,7 @@ type ndBool struct {
}

func (n *ndBool) token() *token { return n.tok }
func (n *ndBool) isexported() bool { return true }
func (n *ndBool) String() string {
return fmt.Sprintf("ndBool{val: %t}", n.val)
}
Expand All @@ -355,6 +375,7 @@ type ndList struct {
}

func (n *ndList) token() *token { return n.tok }
func (n *ndList) isexported() bool { return true }
func (n *ndList) String() string {
return fmt.Sprintf("ndList{vals: %s}", nodesToStr(n.vals))
}
Expand All @@ -366,6 +387,7 @@ type ndDict struct {
}

func (n *ndDict) token() *token { return n.tok }
func (n *ndDict) isexported() bool { return true }
func (n *ndDict) String() string {
return fmt.Sprintf("ndDict{keys: %s, vals: %s}", nodesToStr(n.keys), nodesToStr(n.vals))
}
Expand All @@ -378,6 +400,7 @@ type ndStructDef struct {
}

func (n *ndStructDef) token() *token { return n.tok }
func (n *ndStructDef) isexported() bool { return n.name.isexported() }
func (n *ndStructDef) String() string {
return fmt.Sprintf("ndStructDef{name: %s, vars: %s, fns: %s}", n.name, nodesToStr(n.vars), nodesToStr(n.fns))
}
Expand All @@ -389,6 +412,7 @@ type ndStructInit struct {
}

func (n *ndStructInit) token() *token { return n.tok }
func (n *ndStructInit) isexported() bool { return n.name.isexported() }
func (n *ndStructInit) String() string {
return fmt.Sprintf("ndStructInit{name: %s, values: %s}", n.name, n.values)
}
Expand All @@ -398,6 +422,7 @@ type ndContinue struct {
}

func (n *ndContinue) token() *token { return n.tok }
func (n *ndContinue) isexported() bool { return false }
func (n *ndContinue) String() string {
return "ndContinue{}"
}
Expand All @@ -407,6 +432,7 @@ type ndBreak struct {
}

func (n *ndBreak) token() *token { return n.tok }
func (n *ndBreak) isexported() bool { return false }
func (n *ndBreak) String() string {
return "ndBreak{}"
}
Expand All @@ -417,6 +443,7 @@ type ndReturn struct {
}

func (n *ndReturn) token() *token { return n.tok }
func (n *ndReturn) isexported() bool { return false }
func (n *ndReturn) String() string {
return fmt.Sprintf("ndReturn{val: %s}", n.val)
}
Expand All @@ -427,6 +454,7 @@ type ndImport struct {
}

func (n *ndImport) token() *token { return n.tok }
func (n *ndImport) isexported() bool { return false }
func (n *ndImport) String() string {
return fmt.Sprintf("ndImport{target: %s}", n.target)
}
Expand All @@ -451,3 +479,7 @@ func nodesToStr(nodes []node) string {

return s
}

func isexported(s string) bool {
return unicode.IsUpper(rune(s[0]))
}
4 changes: 4 additions & 0 deletions process.go
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,10 @@ func procSlice(mod *module, n *ndSlice) (procResult, shibaErr) {
}

func procSelector(mod *module, n *ndSelector) (procResult, shibaErr) {
if !n.target.isexported() {
return nil, newsberr(n, "%s is unexported", n.target)
}

selector, err := procAsObj(mod, n.selector)
if err != nil {
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion tests/arith.sb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import assert

as = assert.assert
as = assert.Assert

as(6, 4 + 2)
as(2, 4 - 2)
Expand Down
2 changes: 1 addition & 1 deletion tests/assert.sb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
def assert(expected, actual) {
def Assert(expected, actual) {
if expected != actual {
print("test failed. expected: ", expected, ", actual: " , actual)
exit(1)
Expand Down
2 changes: 1 addition & 1 deletion tests/assign.sb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import assert

as = assert.assert
as = assert.Assert

as(6, 4 + 2)

Expand Down
2 changes: 1 addition & 1 deletion tests/binary.sb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import assert

as = assert.assert
as = assert.Assert

as(false, 4 == 2)
as(false, 4 == 4.0)
Expand Down
2 changes: 1 addition & 1 deletion tests/def.sb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import assert

as = assert.assert
as = assert.Assert

def f(a, b) {
as(1, a)
Expand Down
2 changes: 1 addition & 1 deletion tests/dict.sb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import assert

as = assert.assert
as = assert.Assert

d = {}
as({}, d)
Expand Down
2 changes: 1 addition & 1 deletion tests/for.sb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import assert

as = assert.assert
as = assert.Assert

a = 1

Expand Down
6 changes: 3 additions & 3 deletions tests/gostd.sb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import assert

as = assert.assert
as = assert.Assert

import math
as(6, math.add(4, 2))
as(3.14159265358979323846264338327950288419716939937510582097494459, math.pi)
as(6, math.Add(4, 2))
as(3.14159265358979323846264338327950288419716939937510582097494459, math.Pi)

print("gostd test succeeded")
2 changes: 1 addition & 1 deletion tests/if.sb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import assert

as = assert.assert
as = assert.Assert

a = 1

Expand Down
12 changes: 6 additions & 6 deletions tests/imp.sb
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import imp_2

v = 1
V = 1

def f() {
def F() {
return 2
}

def g() {
return imp_2.v
def G() {
return imp_2.V
}

def h() {
return imp_2.h()
def H() {
return imp_2.H()
}
6 changes: 3 additions & 3 deletions tests/imp_2.sb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import imp_sub/imp_3

v = 3
V = 3

def h() {
return imp_3.h()
def H() {
return imp_3.H()
}
2 changes: 1 addition & 1 deletion tests/imp_4.sb
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v = 4
V = 4
Loading

0 comments on commit 77ebdb7

Please sign in to comment.