Skip to content

Commit 994fd02

Browse files
Move context type to its own file in python library
1 parent 09bfbef commit 994fd02

6 files changed

Lines changed: 79 additions & 79 deletions

File tree

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from typing import Any
2+
from dataclasses import dataclass, field
3+
import uuid
4+
import time
5+
6+
7+
@dataclass
8+
class Context:
9+
vars: dict[str, Any] = field(default_factory=dict)
10+
11+
def set_var(self, name: str, value: Any) -> None:
12+
self.vars[name] = value
13+
14+
def get_var(self, name: str) -> Any:
15+
return self.vars.get(name)
16+
17+
def gen_key(self) -> str:
18+
return str(uuid.uuid4())
19+
20+
def millis_now(self) -> int:
21+
return int(time.time() * 1000)

pkg/runtimes/dython/deepslate/types.py

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,13 @@
11
from enum import IntEnum
22
from typing import Protocol, Any, Optional
33
from dataclasses import dataclass, field
4-
import uuid
5-
import time
64

75
import msgpack
86

7+
from deepslate.context import Context
98
from deepslate.errors import DSError
109

1110

12-
@dataclass
13-
class Context:
14-
vars: dict[str, Any] = field(default_factory=dict)
15-
16-
def set_var(self, name: str, value: Any) -> None:
17-
self.vars[name] = value
18-
19-
def get_var(self, name: str) -> Any:
20-
return self.vars.get(name)
21-
22-
def gen_key(self) -> str:
23-
return str(uuid.uuid4())
24-
25-
def millis_now(self) -> int:
26-
return int(time.time() * 1000)
27-
28-
2911
class Event(Protocol):
3012
def key(self) -> str: ...
3113

pkg/transpilers/dupher/constructors.go

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ import (
44
"fmt"
55
"strconv"
66
"strings"
7-
8-
"github.com/RainingComputers/deepslate/pkg/transpilers"
97
)
108

119
func bodyWrap(stmts []string, depth int, label string, line int) string {
@@ -254,38 +252,3 @@ func emptyBody(line int) string {
254252
func gotoWrap(name string, line int) string {
255253
return fmt.Sprintf("ds.Goto(%d, \"%s\")", line, name)
256254
}
257-
258-
func isNilable(typ string) bool {
259-
return typ == "*string" ||
260-
strings.HasPrefix(typ, "[]") ||
261-
strings.HasPrefix(typ, "map[")
262-
}
263-
264-
func rangeElemType(node ForStmtNode, type_ string) (string, error) {
265-
switch {
266-
case strings.HasPrefix(type_, "[]"):
267-
return type_[2:], nil
268-
269-
case strings.HasPrefix(type_, "map["):
270-
inner := type_[4:]
271-
depth := 1
272-
273-
for i, c := range inner {
274-
switch c {
275-
case '[':
276-
depth++
277-
case ']':
278-
depth--
279-
280-
if depth == 0 {
281-
return inner[i+1:], nil
282-
}
283-
}
284-
}
285-
}
286-
287-
return "", &transpilers.TranspileError{
288-
Message: fmt.Sprintf("type %s cannot be used for range", type_),
289-
Node: &node,
290-
}
291-
}

pkg/transpilers/dupher/transpiler.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -786,6 +786,35 @@ func forRangeStmt(ctx *transpileContext, node ForStmtNode, depth int) error {
786786
return nil
787787
}
788788

789+
func rangeElemType(node ForStmtNode, type_ string) (string, error) {
790+
switch {
791+
case strings.HasPrefix(type_, "[]"):
792+
return type_[2:], nil
793+
794+
case strings.HasPrefix(type_, "map["):
795+
inner := type_[4:]
796+
depth := 1
797+
798+
for i, c := range inner {
799+
switch c {
800+
case '[':
801+
depth++
802+
case ']':
803+
depth--
804+
805+
if depth == 0 {
806+
return inner[i+1:], nil
807+
}
808+
}
809+
}
810+
}
811+
812+
return "", &transpilers.TranspileError{
813+
Message: fmt.Sprintf("type %s cannot be used for range", type_),
814+
Node: &node,
815+
}
816+
}
817+
789818
func expr(ctx *transpileContext, node ExprNode) (string, string, error) {
790819
switch exprNode := node.(type) {
791820
case *IdentNode:
@@ -1358,3 +1387,9 @@ func notSameType(lhs, rhs string) bool {
13581387

13591388
return true
13601389
}
1390+
1391+
func isNilable(typ string) bool {
1392+
return typ == "*string" ||
1393+
strings.HasPrefix(typ, "[]") ||
1394+
strings.HasPrefix(typ, "map[")
1395+
}

pkg/transpilers/dython/constructors.go

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ import (
44
"fmt"
55
"strconv"
66
"strings"
7-
8-
"github.com/RainingComputers/deepslate/pkg/transpilers"
97
)
108

119
func indent(depth int) string {
@@ -209,24 +207,3 @@ func emptyBody(line int) string {
209207
func gotoWrap(name string, line int) string {
210208
return fmt.Sprintf("ds.goto(%d, \"%s\")", line, name)
211209
}
212-
213-
func rangeElemType(node ForStmtNode, type_ string) (string, error) {
214-
switch {
215-
case isList(type_):
216-
return type_[5 : len(type_)-1], nil
217-
218-
case isDict(type_):
219-
inner := type_[5 : len(type_)-1]
220-
key, _, found := strings.Cut(inner, ",")
221-
if !found {
222-
return inner, nil
223-
}
224-
225-
return key, nil
226-
}
227-
228-
return "", &transpilers.TranspileError{
229-
Message: fmt.Sprintf("type %s cannot be used for range", type_),
230-
Node: &node,
231-
}
232-
}

pkg/transpilers/dython/transpiler.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package dython
22

33
import (
44
"fmt"
5+
"strings"
56

67
"github.com/RainingComputers/deepslate/pkg/transpilers"
78
)
@@ -577,6 +578,27 @@ func whileStmt(ctx *transpileContext, node WhileStmtNode, depth int) error {
577578
return nil
578579
}
579580

581+
func rangeElemType(node ForStmtNode, type_ string) (string, error) {
582+
switch {
583+
case isList(type_):
584+
return type_[5 : len(type_)-1], nil
585+
586+
case isDict(type_):
587+
inner := type_[5 : len(type_)-1]
588+
key, _, found := strings.Cut(inner, ",")
589+
if !found {
590+
return inner, nil
591+
}
592+
593+
return key, nil
594+
}
595+
596+
return "", &transpilers.TranspileError{
597+
Message: fmt.Sprintf("type %s cannot be used for range", type_),
598+
Node: &node,
599+
}
600+
}
601+
580602
func expr(ctx *transpileContext, node ExprNode) (string, string, error) {
581603
switch n := node.(type) {
582604
case *IdentNode:

0 commit comments

Comments
 (0)