Skip to content

Commit d8cb1ba

Browse files
committed
🧹 all: Clean-up lexing, comments and sample (#121)
This PR contains various minor clean-ups: - lexing: Make empty string lexing consistent and clearer in JS and go - index.js: Group exported funcs, fix comments, remove debug prints - improve "guess my number" sample This merges the following commits: * samples: Improve "Guess my Number" sample * frontend: Clean up index.js * lexing: Clean up empty string literal lexing frontend/courses/sample/tour/rand.evy | 9 +++------ frontend/index.js | 22 +++++++++------------- frontend/module/yace-editor.js | 14 +++++--------- pkg/lexer/lexer.go | 10 +++------- 4 files changed, 20 insertions(+), 35 deletions(-) Pull-Request: #121
2 parents a667f3b + 74007db commit d8cb1ba

File tree

4 files changed

+20
-35
lines changed

4 files changed

+20
-35
lines changed

frontend/courses/sample/tour/rand.evy

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,7 @@ while true
77
guess := readn
88

99
while guess != n
10-
if guess > 10
11-
print guess "Guess lower (1-10)."
12-
else if guess < 1
13-
print guess " Guess higher (1-10)."
14-
else if guess < n
10+
if guess < n
1511
print guess "Guess higher."
1612
else
1713
print guess "Guess lower."
@@ -23,7 +19,8 @@ while true
2319
end
2420

2521
// readn reads a number.
26-
// If you enter a string that is not number
22+
// If the input is not a number readn
23+
// prints an error and repeats prompt.
2724
func readn:num
2825
while true
2926
str := read

frontend/index.js

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,15 @@ function newEvyGo() {
4545
jsError,
4646
evySource,
4747
setEvySource,
48+
afterStop,
49+
registerEventHandler,
50+
// canvas
4851
move,
4952
line,
5053
width,
5154
circle,
5255
rect,
5356
color,
54-
afterStop,
55-
registerEventHandler,
5657
}
5758
const go = new Go() // see wasm_exec.js
5859
go.importObject.env = Object.assign(go.importObject.env, evyEnv)
@@ -318,10 +319,8 @@ async function handleHashChange() {
318319
opts = { unit: "welcome" }
319320
}
320321
if (opts.content) {
321-
console.log("start content update")
322322
const decoded = await decode(opts.content)
323323
editor.update({ value: decoded, errorLines: {} })
324-
console.log("finished content update")
325324
return
326325
}
327326
let crumbs = ["Evy"]
@@ -411,7 +410,7 @@ function transformY(y) {
411410
return scaleY(y + canvas.offset.y)
412411
}
413412

414-
// move is exported to evy go/wasm
413+
// move is exported to evy go/wasm.
415414
function move(x, y) {
416415
movePhysical(transformX(x), transformY(y))
417416
}
@@ -421,7 +420,7 @@ function movePhysical(px, py) {
421420
canvas.y = py
422421
}
423422

424-
// line is exported to evy go/wasm
423+
// line is exported to evy go/wasm.
425424
function line(x2, y2) {
426425
const { ctx, x, y } = canvas
427426
const px2 = transformX(x2)
@@ -433,19 +432,19 @@ function line(x2, y2) {
433432
movePhysical(px2, py2)
434433
}
435434

436-
// color is exported to evy go/wasm
435+
// color is exported to evy go/wasm.
437436
function color(ptr, len) {
438437
const s = memToString(ptr, len)
439438
canvas.ctx.fillStyle = s
440439
canvas.ctx.strokeStyle = s
441440
}
442441

443-
// width is exported to evy go/wasm
442+
// width is exported to evy go/wasm.
444443
function width(n) {
445444
canvas.ctx.lineWidth = scaleX(n)
446445
}
447446

448-
// rect is exported to evy go/wasm
447+
// rect is exported to evy go/wasm.
449448
function rect(dx, dy) {
450449
const { ctx, x, y } = canvas
451450
const sDX = scaleX(dx)
@@ -454,7 +453,7 @@ function rect(dx, dy) {
454453
movePhysical(x + sDX, y + sDY)
455454
}
456455

457-
// circle is exported to evy go/wasm
456+
// circle is exported to evy go/wasm.
458457
function circle(r) {
459458
const { x, y, ctx } = canvas
460459
ctx.beginPath()
@@ -682,7 +681,6 @@ function showConfetti() {
682681
// --- Share / load snippets -------------------------------------------
683682

684683
async function share() {
685-
console.log("share")
686684
await format()
687685
const el = document.querySelector("#modal-share")
688686

@@ -694,7 +692,6 @@ async function share() {
694692
button.onclick = hideModal
695693
el.replaceChildren(msg, button)
696694
showSharing()
697-
console.log("Fix errors first please.")
698695
return
699696
}
700697
const encoded = await encode(editor.value)
@@ -714,7 +711,6 @@ async function share() {
714711
}
715712
el.replaceChildren(msg, input, button)
716713
showSharing()
717-
console.log(encoded)
718714
}
719715

720716
async function encode(input) {

frontend/module/yace-editor.js

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -627,20 +627,16 @@ function readIdent(s, i) {
627627
}
628628

629629
function readString(s, i) {
630-
let backslashCnt = 0
630+
let escaped = false
631631
while (i < s.length) {
632632
const c = s[i]
633-
if (c === "\\") {
634-
backslashCnt++
635-
} else {
636-
backslashCnt = 0
637-
}
638-
if (c === '"' && backslashCnt % 2 == 0) {
639-
return i + 1
640-
}
641633
if (c === "\n") {
642634
return i
643635
}
636+
if (c === '"' && !escaped) {
637+
return i + 1
638+
}
639+
escaped = c === "\\" && !escaped
644640
i++
645641
}
646642
return i

pkg/lexer/lexer.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -191,15 +191,11 @@ func (l *Lexer) readIdent() string {
191191

192192
func (l *Lexer) readString() (string, error) {
193193
pos := l.pos
194-
backslashCnt := 0
194+
escaped := false
195195
for {
196-
if l.cur == '\\' {
197-
backslashCnt++
198-
} else {
199-
backslashCnt = 0
200-
}
196+
escaped = l.cur == '\\' && !escaped
201197
pr := l.peekRune()
202-
if pr == '"' && backslashCnt%2 == 0 {
198+
if pr == '"' && !escaped {
203199
l.advance() // end of string
204200
break
205201
}

0 commit comments

Comments
 (0)