Skip to content

Commit 7429d85

Browse files
committed
add to access the property of object by "."
1 parent 31b14ae commit 7429d85

File tree

4 files changed

+40
-16
lines changed

4 files changed

+40
-16
lines changed

Parser.js

+19-9
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ const reserved_none_blacketmode = [
1313
];
1414

1515
const isNumber = (c) => "0123456789".indexOf(c) >= 0;
16-
const isOperator = (c) => "+-*/%=!<>,.:".indexOf(c) >= 0;
16+
const isSymbol = (c) => "{}()[].:".indexOf(c) >= 0;
17+
const isOperator = (c) => "+-*/%=!<>,".indexOf(c) >= 0;
1718
const isWhite = (c) => " \t\n".indexOf(c) >= 0;
1819

1920
export class Parser {
@@ -93,7 +94,7 @@ export class Parser {
9394
return { pos, type: "var", name: "print" };
9495
} else if (c == "#") {
9596
state = STATE_COMMENT;
96-
} else if (c == "{" || c == "}" || c == "(" || c == ")" || c == "[" || c == "]") {
97+
} else if (isSymbol(c)) {
9798
return { pos, type: c };
9899
} else if (c == '"') {
99100
state = STATE_STRING;
@@ -108,7 +109,7 @@ export class Parser {
108109
state = STATE_WORD;
109110
}
110111
} else if (state == STATE_WORD) {
111-
if (c == " " || c == "\t" || c == "\n" || isOperator(c) || c == "(" || c == ")" || c == "[" || c == "]" || c === undefined) {
112+
if (c == " " || c == "\t" || c == "\n" || isOperator(c) || isSymbol(c) || c === undefined) {
112113
this.p--;
113114
const w = res.join("");
114115
if (this.reserved.indexOf(w) >= 0) {
@@ -281,7 +282,7 @@ export class Parser {
281282
if (t2.type != "var") throw new Error("オブジェクトの定義には名前が必要です");
282283
const name = t2.name;
283284
const t4 = this.getToken();
284-
if (t4.operator != ":") throw new Error("オブジェクトの定義は名前の後に : が必要です");
285+
if (t4.type != ":") throw new Error("オブジェクトの定義は名前の後に : が必要です");
285286
const value = this.getExpression();
286287
properties.push({
287288
type: "Property",
@@ -449,14 +450,23 @@ export class Parser {
449450

450451
const array = [];
451452
for (;;) {
452-
if (op.type != "[") {
453+
if (op.type != "[" && op.type != ".") {
453454
this.backToken(op);
454455
break;
455456
}
456-
const idx = this.getExpression();
457-
const op2 = this.getToken();
458-
if (op2.type != "]") throw new Error("配列の要素指定が ] で囲われていません");
459-
array.push(idx);
457+
if (op.type == "[") {
458+
const idx = this.getExpression();
459+
const op2 = this.getToken();
460+
if (op2.type != "]") throw new Error("配列の要素指定が ] で囲われていません");
461+
array.push(idx);
462+
} else {
463+
const id = this.getToken();
464+
if (id.type != "var") throw new Error("オブジェクトの要素指定が名前ではありません");
465+
array.push({
466+
type: "Identifier",
467+
name: id.name,
468+
});
469+
}
460470
op = this.getToken();
461471
}
462472
if (array.length == 0) {

Runtime.js

+14-4
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,11 @@ export class Runtime {
116116
let ast = cmd.left;
117117
for (;;) {
118118
if (ast.type == "MemberExpression") {
119-
idxes.unshift(await this.calcExpression(ast.property, scope));
119+
if (ast.property.type == "Identifier") {
120+
idxes.unshift(ast.property.name);
121+
} else {
122+
idxes.unshift(await this.calcExpression(ast.property, scope));
123+
}
120124
ast = ast.object;
121125
} else if (ast.type == "Identifier") {
122126
const name = ast.name;
@@ -140,13 +144,15 @@ export class Runtime {
140144
} else {
141145
const idx = idxes[0];
142146
if (v[idx] === undefined) {
143-
v[idx] = [];
147+
//console.log("1", typeof idx == "number", idx)
148+
v[idx] = typeof idx == "number" ? [] : {};
144149
}
145150
let ret = v[idx];
146151
for (let i = 1; i < idxes.length - 1; i++) {
147152
const idx = idxes[i];
148153
if (ret[idx] === undefined) {
149-
ret[idx] = [];
154+
//console.log("2", typeof idx == "number", idx)
155+
ret[idx] = typeof idx == "number" ? [] : {};
150156
}
151157
ret = ret[idx];
152158
}
@@ -337,7 +343,11 @@ export class Runtime {
337343
const idxes = [];
338344
for (;;) {
339345
if (ast.type == "MemberExpression") {
340-
idxes.unshift(await this.calcExpression(ast.property, scope));
346+
if (ast.property.type == "Identifier") {
347+
idxes.unshift(ast.property.name);
348+
} else {
349+
idxes.unshift(await this.calcExpression(ast.property, scope));
350+
}
341351
ast = ast.object;
342352
} else if (ast.type == "Identifier") {
343353
const name = ast.name;

examples.csv

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,4 @@ commontest2025-1_ja.dncl,23. common test 2025 1 (日本語変数)
2525
commontest2025-2.dncl,24. common test 2025 2
2626
commontest2025-2_ja.dncl,25. common test 2025 2 (日本語変数)
2727
import.dncl,26. import functions
28-
object.dncl,27. オブジェクトの生成
28+
object.dncl,27. オブジェクトの生成とアクセス

examples/object.dncl

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
1-
# 27. オブジェクトの生成
2-
a = { a: 3, b: "ABC" }
1+
# 27. オブジェクトの生成とアクセス
2+
a = { a: 15, b: "ABC" }
3+
a.d = [1, 2, 3, 4]
4+
a.d[0] = 100
5+
a.c = "xyz"
6+
print a.a, a["b"], a.c, a.d.length

0 commit comments

Comments
 (0)