Skip to content

Commit

Permalink
finally, lisp macros!
Browse files Browse the repository at this point in the history
  • Loading branch information
evanbowman committed Nov 20, 2021
1 parent 7bbdec3 commit 1a74aad
Show file tree
Hide file tree
Showing 11 changed files with 1,254 additions and 630 deletions.
45 changes: 42 additions & 3 deletions scripts/init.lisp
Original file line number Diff line number Diff line change
@@ -1,4 +1,43 @@

(macro or (expr)
(if expr
`(if ,(car expr)
1
,(if (cdr expr)
(cons 'or (cdr expr))
0))
0))


(macro and (expr)
(if expr
`(if (not ,(car expr))
0
,(if (cdr expr)
(cons 'and (cdr expr))
1))
1))


;; Because we're running lisp in an embedded system (a gameboy) with limited
;; memory, we need to be really careful about symbol table usage, which is why,
;; traditionally, we only support numbered arguments for lambdas. But this
;; function macro allows you to declare functions with named arguments:
(macro fn (args body)
(if (not args)
`(lambda ,@body)
`(lambda
(let ,((lambda
(if (not $0)
$1
((this)
(cdr $0)
(cons (list (car $0) (symbol (string "$" $2))) $1)
(+ $2 1))))
args nil 0)
,@body))))


;; For our onscreen keyboard, which does not include +, *, or /
(set 'add +)
(set 'mul *)
Expand All @@ -7,8 +46,8 @@

(set 'languages
;; We don't support chinese text on the Desktop or PSP yet.
(if (all-true (not (equal (platform) 'SonyPSP))
(not (equal (platform) 'Desktop)))
(if (and (not (equal (platform) 'SonyPSP))
(not (equal (platform) 'Desktop)))
'(null
(english 1) ;; (language-name preferred-font-size)
(chinese 2)
Expand Down Expand Up @@ -64,7 +103,7 @@
'waypoint-clear-hooks
(compile
(lambda
(if (all-true (not (peer-conn)) (> swarm -1))
(if (and (not (peer-conn)) (> swarm -1))
(progn
;; send notification, swarm approaching
(alert 137)
Expand Down
8 changes: 4 additions & 4 deletions scripts/post_levelgen.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,22 @@
;; Sometime, spawn a swarm of enemies after clearing a waypoint. The
;; waypoint_clear hook will look for the swarm variable in the environment.
(if (equal (cr-choice 4) 0)
(if (all-true
(if (and
(> (level) 1)
(not (equal (level) boss-0-level))
(not (equal (level) boss-1-level))
(not (equal (level) boss-2-level))
(not (equal (level) boss-3-level)))
(set 'swarm (if (all-true (> (level) boss-0-level)
(set 'swarm (if (and (> (level) boss-0-level)
(equal (cr-choice 3) 0))
enemy-scarecrow
enemy-drone))))


;; The only way that we could end up in this scenario, is if we previously died
;; on a swarm level.
(if (all-true (equal (level) 0)
(> swarm -1))
(if (and (equal (level) 0)
(> swarm -1))
(set 'swarm -1))


Expand Down
1 change: 1 addition & 0 deletions source/blind_jump/entity/enemies/enemy.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "blind_jump/entity/entity.hpp"
#include "blind_jump/network_event.hpp"
#include "collision.hpp"
#include "script/lisp.hpp"


class Player;
Expand Down
669 changes: 373 additions & 296 deletions source/data/file_init.cpp

Large diffs are not rendered by default.

151 changes: 75 additions & 76 deletions source/data/file_post_levelgen.cpp
Original file line number Diff line number Diff line change
@@ -1,77 +1,76 @@
extern const unsigned char file_post_levelgen[] = {
0x3b, 0x3b, 0x3b, 0x0a, 0x3b, 0x3b, 0x3b, 0x20, 0x54, 0x68, 0x69, 0x73,
0x20, 0x68, 0x6f, 0x6f, 0x6b, 0x20, 0x77, 0x69, 0x6c, 0x6c, 0x20, 0x62,
0x65, 0x20, 0x65, 0x76, 0x61, 0x6c, 0x75, 0x61, 0x74, 0x65, 0x64, 0x20,
0x77, 0x68, 0x65, 0x6e, 0x20, 0x74, 0x68, 0x65, 0x20, 0x67, 0x61, 0x6d,
0x65, 0x20, 0x66, 0x69, 0x6e, 0x69, 0x73, 0x68, 0x65, 0x73, 0x20, 0x67,
0x65, 0x6e, 0x65, 0x72, 0x61, 0x74, 0x69, 0x6e, 0x67, 0x20, 0x61, 0x20,
0x6e, 0x65, 0x77, 0x20, 0x6c, 0x65, 0x76, 0x65, 0x6c, 0x2e, 0x0a, 0x3b,
0x3b, 0x3b, 0x0a, 0x0a, 0x0a, 0x3b, 0x3b, 0x20, 0x53, 0x6f, 0x6d, 0x65,
0x74, 0x69, 0x6d, 0x65, 0x2c, 0x20, 0x73, 0x70, 0x61, 0x77, 0x6e, 0x20,
0x61, 0x20, 0x73, 0x77, 0x61, 0x72, 0x6d, 0x20, 0x6f, 0x66, 0x20, 0x65,
0x6e, 0x65, 0x6d, 0x69, 0x65, 0x73, 0x20, 0x61, 0x66, 0x74, 0x65, 0x72,
0x20, 0x63, 0x6c, 0x65, 0x61, 0x72, 0x69, 0x6e, 0x67, 0x20, 0x61, 0x20,
0x77, 0x61, 0x79, 0x70, 0x6f, 0x69, 0x6e, 0x74, 0x2e, 0x20, 0x54, 0x68,
0x65, 0x0a, 0x3b, 0x3b, 0x20, 0x77, 0x61, 0x79, 0x70, 0x6f, 0x69, 0x6e,
0x74, 0x5f, 0x63, 0x6c, 0x65, 0x61, 0x72, 0x20, 0x68, 0x6f, 0x6f, 0x6b,
0x20, 0x77, 0x69, 0x6c, 0x6c, 0x20, 0x6c, 0x6f, 0x6f, 0x6b, 0x20, 0x66,
0x6f, 0x72, 0x20, 0x74, 0x68, 0x65, 0x20, 0x73, 0x77, 0x61, 0x72, 0x6d,
0x20, 0x76, 0x61, 0x72, 0x69, 0x61, 0x62, 0x6c, 0x65, 0x20, 0x69, 0x6e,
0x20, 0x74, 0x68, 0x65, 0x20, 0x65, 0x6e, 0x76, 0x69, 0x72, 0x6f, 0x6e,
0x6d, 0x65, 0x6e, 0x74, 0x2e, 0x0a, 0x28, 0x69, 0x66, 0x20, 0x28, 0x65,
0x71, 0x75, 0x61, 0x6c, 0x20, 0x28, 0x63, 0x72, 0x2d, 0x63, 0x68, 0x6f,
0x69, 0x63, 0x65, 0x20, 0x34, 0x29, 0x20, 0x30, 0x29, 0x0a, 0x20, 0x20,
0x20, 0x20, 0x28, 0x69, 0x66, 0x20, 0x28, 0x61, 0x6c, 0x6c, 0x2d, 0x74,
0x72, 0x75, 0x65, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
0x20, 0x28, 0x3e, 0x20, 0x28, 0x6c, 0x65, 0x76, 0x65, 0x6c, 0x29, 0x20,
0x31, 0x29, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
0x28, 0x6e, 0x6f, 0x74, 0x20, 0x28, 0x65, 0x71, 0x75, 0x61, 0x6c, 0x20,
0x28, 0x6c, 0x65, 0x76, 0x65, 0x6c, 0x29, 0x20, 0x62, 0x6f, 0x73, 0x73,
0x2d, 0x30, 0x2d, 0x6c, 0x65, 0x76, 0x65, 0x6c, 0x29, 0x29, 0x0a, 0x20,
0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x28, 0x6e, 0x6f, 0x74,
0x20, 0x28, 0x65, 0x71, 0x75, 0x61, 0x6c, 0x20, 0x28, 0x6c, 0x65, 0x76,
0x65, 0x6c, 0x29, 0x20, 0x62, 0x6f, 0x73, 0x73, 0x2d, 0x31, 0x2d, 0x6c,
0x65, 0x76, 0x65, 0x6c, 0x29, 0x29, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20,
0x20, 0x20, 0x20, 0x20, 0x28, 0x6e, 0x6f, 0x74, 0x20, 0x28, 0x65, 0x71,
0x75, 0x61, 0x6c, 0x20, 0x28, 0x6c, 0x65, 0x76, 0x65, 0x6c, 0x29, 0x20,
0x62, 0x6f, 0x73, 0x73, 0x2d, 0x32, 0x2d, 0x6c, 0x65, 0x76, 0x65, 0x6c,
0x29, 0x29, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
0x28, 0x6e, 0x6f, 0x74, 0x20, 0x28, 0x65, 0x71, 0x75, 0x61, 0x6c, 0x20,
0x28, 0x6c, 0x65, 0x76, 0x65, 0x6c, 0x29, 0x20, 0x62, 0x6f, 0x73, 0x73,
0x2d, 0x33, 0x2d, 0x6c, 0x65, 0x76, 0x65, 0x6c, 0x29, 0x29, 0x29, 0x0a,
0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x28, 0x73, 0x65, 0x74,
0x20, 0x27, 0x73, 0x77, 0x61, 0x72, 0x6d, 0x20, 0x28, 0x69, 0x66, 0x20,
0x28, 0x61, 0x6c, 0x6c, 0x2d, 0x74, 0x72, 0x75, 0x65, 0x20, 0x28, 0x3e,
0x20, 0x28, 0x6c, 0x65, 0x76, 0x65, 0x6c, 0x29, 0x20, 0x62, 0x6f, 0x73,
0x73, 0x2d, 0x30, 0x2d, 0x6c, 0x65, 0x76, 0x65, 0x6c, 0x29, 0x0a, 0x20,
0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x28, 0x65, 0x71,
0x75, 0x61, 0x6c, 0x20, 0x28, 0x63, 0x72, 0x2d, 0x63, 0x68, 0x6f, 0x69,
0x63, 0x65, 0x20, 0x33, 0x29, 0x20, 0x30, 0x29, 0x29, 0x0a, 0x20, 0x20,
0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x65, 0x6e,
0x65, 0x6d, 0x79, 0x2d, 0x73, 0x63, 0x61, 0x72, 0x65, 0x63, 0x72, 0x6f,
0x77, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
0x20, 0x20, 0x65, 0x6e, 0x65, 0x6d, 0x79, 0x2d, 0x64, 0x72, 0x6f, 0x6e,
0x65, 0x29, 0x29, 0x29, 0x29, 0x0a, 0x0a, 0x0a, 0x3b, 0x3b, 0x20, 0x54,
0x68, 0x65, 0x20, 0x6f, 0x6e, 0x6c, 0x79, 0x20, 0x77, 0x61, 0x79, 0x20,
0x74, 0x68, 0x61, 0x74, 0x20, 0x77, 0x65, 0x20, 0x63, 0x6f, 0x75, 0x6c,
0x64, 0x20, 0x65, 0x6e, 0x64, 0x20, 0x75, 0x70, 0x20, 0x69, 0x6e, 0x20,
0x74, 0x68, 0x69, 0x73, 0x20, 0x73, 0x63, 0x65, 0x6e, 0x61, 0x72, 0x69,
0x6f, 0x2c, 0x20, 0x69, 0x73, 0x20, 0x69, 0x66, 0x20, 0x77, 0x65, 0x20,
0x70, 0x72, 0x65, 0x76, 0x69, 0x6f, 0x75, 0x73, 0x6c, 0x79, 0x20, 0x64,
0x69, 0x65, 0x64, 0x0a, 0x3b, 0x3b, 0x20, 0x6f, 0x6e, 0x20, 0x61, 0x20,
0x73, 0x77, 0x61, 0x72, 0x6d, 0x20, 0x6c, 0x65, 0x76, 0x65, 0x6c, 0x2e,
0x0a, 0x28, 0x69, 0x66, 0x20, 0x28, 0x61, 0x6c, 0x6c, 0x2d, 0x74, 0x72,
0x75, 0x65, 0x20, 0x28, 0x65, 0x71, 0x75, 0x61, 0x6c, 0x20, 0x28, 0x6c,
0x65, 0x76, 0x65, 0x6c, 0x29, 0x20, 0x30, 0x29, 0x0a, 0x20, 0x20, 0x20,
0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x28,
0x3e, 0x20, 0x73, 0x77, 0x61, 0x72, 0x6d, 0x20, 0x2d, 0x31, 0x29, 0x29,
0x0a, 0x20, 0x20, 0x20, 0x20, 0x28, 0x73, 0x65, 0x74, 0x20, 0x27, 0x73,
0x77, 0x61, 0x72, 0x6d, 0x20, 0x2d, 0x31, 0x29, 0x29, 0x0a, 0x0a, 0x0a,
0x28, 0x6d, 0x61, 0x70, 0x20, 0x28, 0x6c, 0x61, 0x6d, 0x62, 0x64, 0x61,
0x20, 0x28, 0x24, 0x30, 0x29, 0x29, 0x20, 0x70, 0x6f, 0x73, 0x74, 0x2d,
0x6c, 0x65, 0x76, 0x65, 0x6c, 0x67, 0x65, 0x6e, 0x2d, 0x68, 0x6f, 0x6f,
0x6b, 0x73, 0x29, 0x0a, 0x00};
0x3b, 0x3b, 0x3b, 0x0a, 0x3b, 0x3b, 0x3b, 0x20, 0x54, 0x68, 0x69, 0x73,
0x20, 0x68, 0x6f, 0x6f, 0x6b, 0x20, 0x77, 0x69, 0x6c, 0x6c, 0x20, 0x62,
0x65, 0x20, 0x65, 0x76, 0x61, 0x6c, 0x75, 0x61, 0x74, 0x65, 0x64, 0x20,
0x77, 0x68, 0x65, 0x6e, 0x20, 0x74, 0x68, 0x65, 0x20, 0x67, 0x61, 0x6d,
0x65, 0x20, 0x66, 0x69, 0x6e, 0x69, 0x73, 0x68, 0x65, 0x73, 0x20, 0x67,
0x65, 0x6e, 0x65, 0x72, 0x61, 0x74, 0x69, 0x6e, 0x67, 0x20, 0x61, 0x20,
0x6e, 0x65, 0x77, 0x20, 0x6c, 0x65, 0x76, 0x65, 0x6c, 0x2e, 0x0a, 0x3b,
0x3b, 0x3b, 0x0a, 0x0a, 0x0a, 0x3b, 0x3b, 0x20, 0x53, 0x6f, 0x6d, 0x65,
0x74, 0x69, 0x6d, 0x65, 0x2c, 0x20, 0x73, 0x70, 0x61, 0x77, 0x6e, 0x20,
0x61, 0x20, 0x73, 0x77, 0x61, 0x72, 0x6d, 0x20, 0x6f, 0x66, 0x20, 0x65,
0x6e, 0x65, 0x6d, 0x69, 0x65, 0x73, 0x20, 0x61, 0x66, 0x74, 0x65, 0x72,
0x20, 0x63, 0x6c, 0x65, 0x61, 0x72, 0x69, 0x6e, 0x67, 0x20, 0x61, 0x20,
0x77, 0x61, 0x79, 0x70, 0x6f, 0x69, 0x6e, 0x74, 0x2e, 0x20, 0x54, 0x68,
0x65, 0x0a, 0x3b, 0x3b, 0x20, 0x77, 0x61, 0x79, 0x70, 0x6f, 0x69, 0x6e,
0x74, 0x5f, 0x63, 0x6c, 0x65, 0x61, 0x72, 0x20, 0x68, 0x6f, 0x6f, 0x6b,
0x20, 0x77, 0x69, 0x6c, 0x6c, 0x20, 0x6c, 0x6f, 0x6f, 0x6b, 0x20, 0x66,
0x6f, 0x72, 0x20, 0x74, 0x68, 0x65, 0x20, 0x73, 0x77, 0x61, 0x72, 0x6d,
0x20, 0x76, 0x61, 0x72, 0x69, 0x61, 0x62, 0x6c, 0x65, 0x20, 0x69, 0x6e,
0x20, 0x74, 0x68, 0x65, 0x20, 0x65, 0x6e, 0x76, 0x69, 0x72, 0x6f, 0x6e,
0x6d, 0x65, 0x6e, 0x74, 0x2e, 0x0a, 0x28, 0x69, 0x66, 0x20, 0x28, 0x65,
0x71, 0x75, 0x61, 0x6c, 0x20, 0x28, 0x63, 0x72, 0x2d, 0x63, 0x68, 0x6f,
0x69, 0x63, 0x65, 0x20, 0x34, 0x29, 0x20, 0x30, 0x29, 0x0a, 0x20, 0x20,
0x20, 0x20, 0x28, 0x69, 0x66, 0x20, 0x28, 0x61, 0x6e, 0x64, 0x0a, 0x20,
0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x28, 0x3e, 0x20, 0x28,
0x6c, 0x65, 0x76, 0x65, 0x6c, 0x29, 0x20, 0x31, 0x29, 0x0a, 0x20, 0x20,
0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x28, 0x6e, 0x6f, 0x74, 0x20,
0x28, 0x65, 0x71, 0x75, 0x61, 0x6c, 0x20, 0x28, 0x6c, 0x65, 0x76, 0x65,
0x6c, 0x29, 0x20, 0x62, 0x6f, 0x73, 0x73, 0x2d, 0x30, 0x2d, 0x6c, 0x65,
0x76, 0x65, 0x6c, 0x29, 0x29, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
0x20, 0x20, 0x20, 0x28, 0x6e, 0x6f, 0x74, 0x20, 0x28, 0x65, 0x71, 0x75,
0x61, 0x6c, 0x20, 0x28, 0x6c, 0x65, 0x76, 0x65, 0x6c, 0x29, 0x20, 0x62,
0x6f, 0x73, 0x73, 0x2d, 0x31, 0x2d, 0x6c, 0x65, 0x76, 0x65, 0x6c, 0x29,
0x29, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x28,
0x6e, 0x6f, 0x74, 0x20, 0x28, 0x65, 0x71, 0x75, 0x61, 0x6c, 0x20, 0x28,
0x6c, 0x65, 0x76, 0x65, 0x6c, 0x29, 0x20, 0x62, 0x6f, 0x73, 0x73, 0x2d,
0x32, 0x2d, 0x6c, 0x65, 0x76, 0x65, 0x6c, 0x29, 0x29, 0x0a, 0x20, 0x20,
0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x28, 0x6e, 0x6f, 0x74, 0x20,
0x28, 0x65, 0x71, 0x75, 0x61, 0x6c, 0x20, 0x28, 0x6c, 0x65, 0x76, 0x65,
0x6c, 0x29, 0x20, 0x62, 0x6f, 0x73, 0x73, 0x2d, 0x33, 0x2d, 0x6c, 0x65,
0x76, 0x65, 0x6c, 0x29, 0x29, 0x29, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20,
0x20, 0x20, 0x20, 0x28, 0x73, 0x65, 0x74, 0x20, 0x27, 0x73, 0x77, 0x61,
0x72, 0x6d, 0x20, 0x28, 0x69, 0x66, 0x20, 0x28, 0x61, 0x6e, 0x64, 0x20,
0x28, 0x3e, 0x20, 0x28, 0x6c, 0x65, 0x76, 0x65, 0x6c, 0x29, 0x20, 0x62,
0x6f, 0x73, 0x73, 0x2d, 0x30, 0x2d, 0x6c, 0x65, 0x76, 0x65, 0x6c, 0x29,
0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x28,
0x65, 0x71, 0x75, 0x61, 0x6c, 0x20, 0x28, 0x63, 0x72, 0x2d, 0x63, 0x68,
0x6f, 0x69, 0x63, 0x65, 0x20, 0x33, 0x29, 0x20, 0x30, 0x29, 0x29, 0x0a,
0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
0x65, 0x6e, 0x65, 0x6d, 0x79, 0x2d, 0x73, 0x63, 0x61, 0x72, 0x65, 0x63,
0x72, 0x6f, 0x77, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
0x20, 0x20, 0x20, 0x20, 0x65, 0x6e, 0x65, 0x6d, 0x79, 0x2d, 0x64, 0x72,
0x6f, 0x6e, 0x65, 0x29, 0x29, 0x29, 0x29, 0x0a, 0x0a, 0x0a, 0x3b, 0x3b,
0x20, 0x54, 0x68, 0x65, 0x20, 0x6f, 0x6e, 0x6c, 0x79, 0x20, 0x77, 0x61,
0x79, 0x20, 0x74, 0x68, 0x61, 0x74, 0x20, 0x77, 0x65, 0x20, 0x63, 0x6f,
0x75, 0x6c, 0x64, 0x20, 0x65, 0x6e, 0x64, 0x20, 0x75, 0x70, 0x20, 0x69,
0x6e, 0x20, 0x74, 0x68, 0x69, 0x73, 0x20, 0x73, 0x63, 0x65, 0x6e, 0x61,
0x72, 0x69, 0x6f, 0x2c, 0x20, 0x69, 0x73, 0x20, 0x69, 0x66, 0x20, 0x77,
0x65, 0x20, 0x70, 0x72, 0x65, 0x76, 0x69, 0x6f, 0x75, 0x73, 0x6c, 0x79,
0x20, 0x64, 0x69, 0x65, 0x64, 0x0a, 0x3b, 0x3b, 0x20, 0x6f, 0x6e, 0x20,
0x61, 0x20, 0x73, 0x77, 0x61, 0x72, 0x6d, 0x20, 0x6c, 0x65, 0x76, 0x65,
0x6c, 0x2e, 0x0a, 0x28, 0x69, 0x66, 0x20, 0x28, 0x61, 0x6e, 0x64, 0x20,
0x28, 0x65, 0x71, 0x75, 0x61, 0x6c, 0x20, 0x28, 0x6c, 0x65, 0x76, 0x65,
0x6c, 0x29, 0x20, 0x30, 0x29, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
0x20, 0x20, 0x20, 0x28, 0x3e, 0x20, 0x73, 0x77, 0x61, 0x72, 0x6d, 0x20,
0x2d, 0x31, 0x29, 0x29, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x28, 0x73, 0x65,
0x74, 0x20, 0x27, 0x73, 0x77, 0x61, 0x72, 0x6d, 0x20, 0x2d, 0x31, 0x29,
0x29, 0x0a, 0x0a, 0x0a, 0x28, 0x6d, 0x61, 0x70, 0x20, 0x28, 0x6c, 0x61,
0x6d, 0x62, 0x64, 0x61, 0x20, 0x28, 0x24, 0x30, 0x29, 0x29, 0x20, 0x70,
0x6f, 0x73, 0x74, 0x2d, 0x6c, 0x65, 0x76, 0x65, 0x6c, 0x67, 0x65, 0x6e,
0x2d, 0x68, 0x6f, 0x6f, 0x6b, 0x73, 0x29, 0x0a
, 0x00};
56 changes: 55 additions & 1 deletion source/script/bytecode.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -660,6 +660,57 @@ struct PushString {
};


// NOTE: These relocatable instructions exist for portable bytecode. The
// non-relocatable versions of these instructions refer to an index in the
// interpreter's string intern table. We cannot assume anything about the
// ordering of the string intern table in the interpreter that ultimately loads
// the bytecode, so instead, we ship a symbol table along with the portable
// bytecode. When first seeing this instruction, the VM loads the symbol string
// from the bytecode's symbol table, and searches the host interpreter's symbol
// table for the symbol string. The vm then swaps the relocatable instruction
// for the non-relocatable version, using the symbol offset into the host symbol
// table. Loading relocatable symbols is a bit slow the first time, but
// optimized out after the first load.
struct LoadVarRelocatable : public LoadVar {
static const char* name()
{
return "LOAD_VAR_RELOCATABLE";
}

static constexpr Opcode op()
{
return 43;
}
}; static_assert(sizeof(LoadVarRelocatable) == sizeof(LoadVar));


struct PushSymbolRelocatable : public PushSymbol {
static const char* name()
{
return "PUSH_SYMBOL_RELOCATABLE";
}

static constexpr Opcode op()
{
return 44;
}
}; static_assert(sizeof(PushSymbolRelocatable) == sizeof(PushSymbol));


struct LexicalDefRelocatable : public LexicalDef {
static const char* name()
{
return "LEXICAL_DEF_RELOCATABLE";
}

static constexpr Opcode op()
{
return 45;
}
}; static_assert(sizeof(LexicalDefRelocatable) == sizeof(LexicalDef));



// Just a utility intended for the compiler, not to be used by the vm.
inline Header* load_instruction(ScratchBuffer& buffer, int index)
{
Expand All @@ -676,7 +727,7 @@ inline Header* load_instruction(ScratchBuffer& buffer, int index)
} else {
index--;
offset += sizeof(PushString) +
((PushString*)buffer.data_ + offset)->length_;
((PushString*)(buffer.data_ + offset))->length_;
}
break;

Expand All @@ -691,7 +742,9 @@ inline Header* load_instruction(ScratchBuffer& buffer, int index)
break;

MATCH(LoadVar)
MATCH(LoadVarRelocatable)
MATCH(PushSymbol)
MATCH(PushSymbolRelocatable)
MATCH(PushNil)
MATCH(Push0)
MATCH(Push1)
Expand Down Expand Up @@ -726,6 +779,7 @@ inline Header* load_instruction(ScratchBuffer& buffer, int index)
MATCH(PushThis)
MATCH(Not)
MATCH(LexicalDef)
MATCH(LexicalDefRelocatable)
MATCH(LexicalFramePush)
MATCH(LexicalFramePop)
MATCH(LexicalVarLoad)
Expand Down
Loading

0 comments on commit 1a74aad

Please sign in to comment.