forked from HapticX/happyx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
testjs7.nim
167 lines (138 loc) · 2.82 KB
/
testjs7.nim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import
../src/happyx
var nimVar = 100
buildJs:
# Create class
class Animal:
say():
discard
class Cat extends Animal:
say():
echo "Meow"
class Dog extends Animal:
say():
echo "Woof!"
var dog = new Dog()
var cat = new Cat()
dog.say()
cat.say()
proc myNimFunc(arg: string) =
echo arg
buildJs:
# using nim code inside buildJs
nim:
myNimFunc("Hello!")
# and nested ...
buildJs:
echo "Hello from JS"
nim:
echo "Hello from Nim"
buildJs:
echo "Hello from nested nested Js"
nim:
echo "Hello from nested nested Nim"
buildJs:
# translates into
# let name = 123;
var name = 123
# translates into
# const name1 = 123;
# const name2 = 123;
let name1 = 123
const name2 = 123
# translates into
# let arr = [2, 4, 3, 2, 1]
var arr = [2, 4, 3, 2, 1]
echo arr
# translates into
# for (var i = 0; i < 10; ++i) { ... }
for i in 0..10:
echo i
# translates into
# for ((val, idx) in arr)
for (val, idx) in arr:
echo "val:", val, "and idx:", idx
# translates into
# function fun(a, b, c, d) { ... }
function fun(a, b, c, d):
# translates into
# console.log(a, b, c, d)
console.log(a, b, c, d)
# translates into
# console.log(a, b, c, d)
echo a, b, c, d
fun(5, 1, 2, 3)
# translates into if-else statement
if 5 === 2:
~nimVar = 2
elif 5 === 4:
~nimVar = 4
else:
~nimVar = 5
var x = "hello"
# translates into switch-case statement
case x
of 0:
echo "x is 0"
of 1, 2, 3, 4, 5:
echo "0 <= x <= 5"
of true:
echo "x is true"
else:
echo "x is", x
var a = 0
while a < 10:
++a
if a === 9:
echo a * 1000
discard
discard console.log("Hello, world!")
# translates into
# class Rectangle extends Object { ... }
class Rectangle extends Object:
# translates into
# #privateField
privateField
privateField1 = 100
# translates into
# publicField
pub publicField
pub publicField1 = 100
# translates into
# constructor(a)
constructor(a):
# translates into
# this.publicField = a
super()
self.publicField = a
# translates into
# methodName()
methodName():
echo "Hello, world!"
# Using nim variables:
echo ~nimVar
var rect = new Rectangle(100)
echo rect.a
type
A = enum
One, Two, Three,
Four = 100
B = object
a: int
b: string
c*: seq[string]
var enumA = A.One
eval("console.log('Hello, world!')")
case enumA:
of A.One:
echo "Hi!"
else:
echo "Bye!"
block loop1:
for i in 0..<3:
block loop2:
for j in 0..<3:
if i == 1 && j == 1:
continue loop1
echo "i =", i, "j =", j
echo nimVar