calog/examples/scripts/languages/mybasic.bas

38 lines
1.3 KiB
QBasic

' A guided tour of my-basic running on calog.
' Demonstrates: variables, a FOR..NEXT loop that computes a running sum, an
' IF..THEN..ELSE..ENDIF decision, then the cryptoHashSha256 and kvSet/kvGet
' natives. my-basic has no first-class functions, so there are no callbacks.
' Run: bin/calog examples/scripts/languages/mybasic.bas
' Plain variables. my-basic folds identifiers to a common case at parse time.
language = "my-basic"
upto = 6
total = 0
' A FOR..NEXT loop building the sum 1 + 2 + ... + upto.
for i = 1 to upto
total = total + i
calogPrint("added", i, "running total is", total)
next i
calogPrint("sum of 1..", upto, "=", total)
' An IF..THEN..ELSE..ENDIF decision on the computed sum.
if total > 20 then
calogPrint("total", total, "is greater than 20")
else
calogPrint("total", total, "is 20 or less")
endif
' Hash a string with the crypto native; it returns a hex digest.
message = "calog loves BASIC"
digest = cryptoHashSha256(message)
calogPrint("sha256(" + message + ") =", digest)
' Store the digest in the process-wide key/value store, then read it back.
kvSet("mybasic.digest", digest)
roundTrip = kvGet("mybasic.digest")
calogPrint("kv round-trip matches:", roundTrip = digest)
' Tear down and exit cleanly (required -- calog does not auto-exit).
calogExit(0)