@arwildo/jawascript 1.0.0 → 1.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +8 -12
- package/examples/test.jawa +175 -0
- package/package.json +1 -1
- package/src/translator.js +151 -5
- package/examples/hello.jawa +0 -17
package/README.md
CHANGED
|
@@ -1,13 +1,14 @@
|
|
|
1
|
-
|
|
1
|
+
<p align=center><img src="https://raw.githubusercontent.com/arwildo/jawascript/refs/heads/main/jawascript.png" width="86"></img></p>
|
|
2
2
|
|
|
3
|
+
# JawaScript
|
|
3
4
|
> JavaScript nganggo basa Jawa.
|
|
4
5
|
|
|
5
|
-
|
|
6
|
+
JawaScript adalah **source to source transpiler** kecil yang menerjemahkan file `.jawa` menjadi JavaScript, lalu mengeksekusinya dengan Node.js.
|
|
6
7
|
|
|
7
8
|
## Install
|
|
8
9
|
|
|
9
10
|
```bash
|
|
10
|
-
npm install -g
|
|
11
|
+
npm install -g @arwildo/jawascript
|
|
11
12
|
```
|
|
12
13
|
|
|
13
14
|
## Usage
|
|
@@ -55,17 +56,12 @@ yen (x < y) {
|
|
|
55
56
|
}
|
|
56
57
|
```
|
|
57
58
|
|
|
58
|
-
##
|
|
59
|
-
|
|
60
|
-
| Jawa | JavaScript |
|
|
61
|
-
| -------- | ----------- |
|
|
62
|
-
| ono | let |
|
|
63
|
-
| tampilno | console.log |
|
|
64
|
-
| yen | if |
|
|
65
|
-
| yen ora | else |
|
|
59
|
+
## Keywords
|
|
66
60
|
|
|
61
|
+
[Keywords](keywords.md)
|
|
62
|
+
|
|
67
63
|
## Cara Kerja
|
|
68
64
|
|
|
69
65
|
File `.jawa` dibaca, keyword Jawa diganti dengan JavaScript menggunakan regex word boundary (urutan replacement penting: `yen ora` diproses sebelum `yen`), lalu dieksekusi.
|
|
70
|
-
|
|
66
|
+
JawaScript adalah project eksperimen yang dibuat untuk bersenang senang dan belajar bahasa jawa secara secara programming.
|
|
71
67
|
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
// Hello World ne
|
|
2
|
+
tampilno("piye kabare jagad!");
|
|
3
|
+
|
|
4
|
+
// Variable ne
|
|
5
|
+
ono x = 10;
|
|
6
|
+
ono y = 20;
|
|
7
|
+
|
|
8
|
+
// Print ne
|
|
9
|
+
tampilno(x + y);
|
|
10
|
+
|
|
11
|
+
// If statement ne, ojo lali yo mas e!
|
|
12
|
+
yen (x < y) {
|
|
13
|
+
tampilno("x luwih cilik");
|
|
14
|
+
} yen ora {
|
|
15
|
+
tampilno("x luwih gedhe");
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
// teks dadi integer
|
|
19
|
+
y = teks dadi integer("123");
|
|
20
|
+
|
|
21
|
+
// teks dadi desimal
|
|
22
|
+
x = teks dadi desimal("1.23");
|
|
23
|
+
|
|
24
|
+
// const
|
|
25
|
+
tetep jeneng = "Iron Man";
|
|
26
|
+
|
|
27
|
+
// function
|
|
28
|
+
fungsi tambah(a, b) {
|
|
29
|
+
balekno a + b;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// while
|
|
33
|
+
ono angka = 0;
|
|
34
|
+
nalikane (angka < 5) {
|
|
35
|
+
tampilno(angka);
|
|
36
|
+
angka++;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// for
|
|
40
|
+
kanggo (ono i = 0; i < 5; i++) {
|
|
41
|
+
tampilno("Ojo lali, i = " + i);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// break
|
|
45
|
+
kanggo (ono i = 0; i < 10; i++) {
|
|
46
|
+
yen (i padha karo 5) {
|
|
47
|
+
mandheg;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// continue
|
|
52
|
+
kanggo (ono i = 0; i < 10; i++) {
|
|
53
|
+
yen (i padha karo 5) {
|
|
54
|
+
terusno;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
tampilno("Aku ora ketok angka 5");
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// lan
|
|
61
|
+
yen (x gedhe 1 lan y gedhe 1) {
|
|
62
|
+
tampilno("Loro-lorone bener, ojo lali!");
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// utawa
|
|
66
|
+
yen (x gedhe 100 utawa y gedhe 100) {
|
|
67
|
+
tampilno("Salah siji dadi Iron Man");
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// padha karo
|
|
71
|
+
yen (x padha karo 10) {
|
|
72
|
+
tampilno("10 iku 10, matematika ora iso ngapusi");
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// ora padha
|
|
76
|
+
yen (x ora padha 999) {
|
|
77
|
+
tampilno("x dudu 999");
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// gedhe padha
|
|
81
|
+
yen (x gedhe padha 10) {
|
|
82
|
+
tampilno("x gedhe utawa padha karo 10");
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// cilik padha
|
|
86
|
+
yen (y cilik padha 20) {
|
|
87
|
+
tampilno("y cilik utawa padha karo 20");
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// gedhe
|
|
91
|
+
yen (y gedhe 10) {
|
|
92
|
+
tampilno("y luwih gedhe saka 10");
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// cilik
|
|
96
|
+
yen (x cilik 20) {
|
|
97
|
+
tampilno("x luwih cilik saka 20");
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
// bener
|
|
101
|
+
ono ironMan = bener;
|
|
102
|
+
|
|
103
|
+
// salah
|
|
104
|
+
ono thanos = salah;
|
|
105
|
+
|
|
106
|
+
// kosong
|
|
107
|
+
ono baterei = kosong;
|
|
108
|
+
|
|
109
|
+
// ora ono
|
|
110
|
+
ono avengers = ora ono;
|
|
111
|
+
|
|
112
|
+
// coba
|
|
113
|
+
coba {
|
|
114
|
+
tampilno("Tony Stark lagi ngoding");
|
|
115
|
+
} tangkap (err) {
|
|
116
|
+
tampilno("Waduh, error mas e!");
|
|
117
|
+
} pungkasan {
|
|
118
|
+
tampilno("Ojo lali commit");
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
// uncalno
|
|
122
|
+
fungsi cekArmor(armor) {
|
|
123
|
+
yen (armor cilik 0) {
|
|
124
|
+
uncalno "Armor Iron Man ilang";
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
balekno armor;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
// pilih, kasus, standar
|
|
131
|
+
ono pilihan = 2;
|
|
132
|
+
|
|
133
|
+
pilih (pilihan) {
|
|
134
|
+
kasus 1:
|
|
135
|
+
tampilno("Thor");
|
|
136
|
+
mandheg;
|
|
137
|
+
|
|
138
|
+
kasus 2:
|
|
139
|
+
tampilno("Iron Man");
|
|
140
|
+
mandheg;
|
|
141
|
+
|
|
142
|
+
standar:
|
|
143
|
+
tampilno("Ojo lali, Avengers durung lengkap");
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
// kelas
|
|
147
|
+
kelas Pahlawan {
|
|
148
|
+
konstruktor(nama) {
|
|
149
|
+
this.nama = nama;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
salam() {
|
|
153
|
+
tampilno("Halo, aku " + this.nama);
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
// waris
|
|
158
|
+
kelas IronMan waris Pahlawan {
|
|
159
|
+
konstruktor(nama, armor) {
|
|
160
|
+
super(nama);
|
|
161
|
+
this.armor = armor;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
terbang() {
|
|
165
|
+
tampilno(this.nama + " lagi mabur nganggo armor");
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
// anyar
|
|
170
|
+
ono pahlawan = anyar Pahlawan("Batman");
|
|
171
|
+
pahlawan.salam();
|
|
172
|
+
|
|
173
|
+
ono ironman = anyar IronMan("Iron Man", "Mark 85");
|
|
174
|
+
ironman.salam();
|
|
175
|
+
ironman.terbang();
|
package/package.json
CHANGED
package/src/translator.js
CHANGED
|
@@ -3,19 +3,165 @@ const KEYWORDS = [
|
|
|
3
3
|
["yen", "if"],
|
|
4
4
|
["tampilno", "console.log"],
|
|
5
5
|
["ono", "let"],
|
|
6
|
+
["teks dadi integer", "parseInt"],
|
|
7
|
+
["teks dadi desimal", "parseFloat"],
|
|
8
|
+
["tetep", "const"],
|
|
9
|
+
["fungsi", "function"],
|
|
10
|
+
["balekno", "return"],
|
|
11
|
+
["nalikane", "while"],
|
|
12
|
+
["kanggo", "for"],
|
|
13
|
+
["mandheg", "break"],
|
|
14
|
+
["terusno", "continue"],
|
|
15
|
+
["lan", "&&"],
|
|
16
|
+
["utawa", "||"],
|
|
17
|
+
["padha karo", "==="],
|
|
18
|
+
["ora padha", "!=="],
|
|
19
|
+
["gedhe padha", ">="],
|
|
20
|
+
["cilik padha", "<="],
|
|
21
|
+
["gedhe", ">"],
|
|
22
|
+
["cilik", "<"],
|
|
23
|
+
["bener", "true"],
|
|
24
|
+
["salah", "false"],
|
|
25
|
+
["kosong", "null"],
|
|
26
|
+
["ora ono", "undefined"],
|
|
27
|
+
["coba", "try"],
|
|
28
|
+
["tangkap", "catch"],
|
|
29
|
+
["pungkasan", "finally"],
|
|
30
|
+
["uncalno", "throw"],
|
|
31
|
+
["pilih", "switch"],
|
|
32
|
+
["kasus", "case"],
|
|
33
|
+
["standar", "default"],
|
|
34
|
+
["kelas", "class"],
|
|
35
|
+
["waris", "extends"],
|
|
36
|
+
["konstruktor", "constructor"],
|
|
37
|
+
["anyar", "new"],
|
|
6
38
|
];
|
|
7
39
|
|
|
8
40
|
function escapeRegex(str) {
|
|
9
41
|
return str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
10
42
|
}
|
|
11
43
|
|
|
44
|
+
const SORTED_KEYWORDS = [...KEYWORDS].sort(
|
|
45
|
+
([a], [b]) => b.length - a.length
|
|
46
|
+
);
|
|
47
|
+
|
|
48
|
+
const KEYWORD_REGEX = new RegExp(
|
|
49
|
+
`(?<![A-Za-z0-9_])(?:${SORTED_KEYWORDS
|
|
50
|
+
.map(([from]) => escapeRegex(from))
|
|
51
|
+
.join("|")})(?![A-Za-z0-9_])`,
|
|
52
|
+
"g"
|
|
53
|
+
);
|
|
54
|
+
|
|
55
|
+
function replaceKeywords(text) {
|
|
56
|
+
return text.replace(KEYWORD_REGEX, (match) => {
|
|
57
|
+
const keyword = SORTED_KEYWORDS.find(
|
|
58
|
+
([from]) => from === match
|
|
59
|
+
);
|
|
60
|
+
|
|
61
|
+
return keyword ? keyword[1] : match;
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
|
|
12
65
|
export function translate(code) {
|
|
13
|
-
let
|
|
66
|
+
let output = "";
|
|
67
|
+
let buffer = "";
|
|
68
|
+
|
|
69
|
+
let i = 0;
|
|
70
|
+
let state = "code";
|
|
14
71
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
72
|
+
function flushBuffer() {
|
|
73
|
+
if (buffer.length > 0) {
|
|
74
|
+
output += replaceKeywords(buffer);
|
|
75
|
+
buffer = "";
|
|
76
|
+
}
|
|
18
77
|
}
|
|
19
78
|
|
|
20
|
-
|
|
79
|
+
while (i < code.length) {
|
|
80
|
+
const char = code[i];
|
|
81
|
+
const next = code[i + 1];
|
|
82
|
+
|
|
83
|
+
if (state === "code") {
|
|
84
|
+
if (char === '"' || char === "'" || char === "`") {
|
|
85
|
+
flushBuffer();
|
|
86
|
+
|
|
87
|
+
state = char;
|
|
88
|
+
output += char;
|
|
89
|
+
|
|
90
|
+
i++;
|
|
91
|
+
continue;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
if (char === "/" && next === "/") {
|
|
95
|
+
flushBuffer();
|
|
96
|
+
|
|
97
|
+
state = "line-comment";
|
|
98
|
+
output += "//";
|
|
99
|
+
|
|
100
|
+
i += 2;
|
|
101
|
+
continue;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
if (char === "/" && next === "*") {
|
|
105
|
+
flushBuffer();
|
|
106
|
+
|
|
107
|
+
state = "block-comment";
|
|
108
|
+
output += "/*";
|
|
109
|
+
|
|
110
|
+
i += 2;
|
|
111
|
+
continue;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
buffer += char;
|
|
115
|
+
i++;
|
|
116
|
+
continue;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
if (state === '"' || state === "'" || state === "`") {
|
|
120
|
+
output += char;
|
|
121
|
+
|
|
122
|
+
if (char === "\\") {
|
|
123
|
+
if (next !== undefined) {
|
|
124
|
+
output += next;
|
|
125
|
+
i += 2;
|
|
126
|
+
continue;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
if (char === state) {
|
|
131
|
+
state = "code";
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
i++;
|
|
135
|
+
continue;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
if (state === "line-comment") {
|
|
139
|
+
output += char;
|
|
140
|
+
|
|
141
|
+
if (char === "\n") {
|
|
142
|
+
state = "code";
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
i++;
|
|
146
|
+
continue;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
if (state === "block-comment") {
|
|
150
|
+
output += char;
|
|
151
|
+
|
|
152
|
+
if (char === "*" && next === "/") {
|
|
153
|
+
output += "/";
|
|
154
|
+
state = "code";
|
|
155
|
+
i += 2;
|
|
156
|
+
continue;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
i++;
|
|
160
|
+
continue;
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
flushBuffer();
|
|
165
|
+
|
|
166
|
+
return output;
|
|
21
167
|
}
|
package/examples/hello.jawa
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
// Hello World ne
|
|
2
|
-
tampilno("piye kabare jagad!");
|
|
3
|
-
|
|
4
|
-
// Variable ne
|
|
5
|
-
ono x = 10;
|
|
6
|
-
ono y = 20;
|
|
7
|
-
|
|
8
|
-
// Print ne
|
|
9
|
-
tampilno(x + y);
|
|
10
|
-
|
|
11
|
-
// If statement ne, ojo lali yo mas e!
|
|
12
|
-
yen (x < y) {
|
|
13
|
-
tampilno("x luwih cilik");
|
|
14
|
-
} yen ora {
|
|
15
|
-
tampilno("x luwih gedhe");
|
|
16
|
-
}
|
|
17
|
-
|