• define-syntax problem when generating new functions

    From alicetrillianosako@gmail.com@21:1/5 to All on Thu Oct 1 17:56:21 2020
    Macro definition in Scheme (Guile specifically) continues to confound me, and I am hoping that in posting this, I will get the nudge I need to fix my latest issue.

    I am working on a cross-assembler targeting the 6502 microprocessor (specifically intending it for a new retro-computer named the 'Commander X16', but that's not relevant - I am doing this mainly for my own didactic edification). My intent is that the
    assembler use a set of Scheme functions which would accept the instruction arguments and emit the machine language opcodes out to a file.

    Part of the intent with this is to allow the assembly code - which is, in effect, Scheme code - to be able to use Scheme macros directly in emitting more complex routines. This is a design I mean to apply to other assemblers for more modern ISAs, to
    facilitate OS development in a Scheme dialect to be designed; I am using the simpler 6502 ISA as a test of the method.

    To facilitate the assembler design, I have written a few macros which take the mnemonics names and relevant information about the opcode and code generation, and generate the instruction functions automatically, such that I only need define each category
    of instruction once. While one of these macros is finally working, the one I've been working on currently compiles as itself but returns an error message when I invoke it. Here is a self-contained, compilable sub-set of the code:

    -------------------------------------------------
    (define-syntax define-opcode
    (syntax-rules (arith logic jump ext)
    ((_ <name> <opcode> arith)
    (define (<name> subop value)
    (let* ((computed-subop
    (case (subop)
    ((z-indirect-x) (inexact->exact #b00000))
    ((z-page) (inexact->exact #b00100))
    ((abs) (inexact->exact #b01100))
    ((z-indirect-y) (inexact->exact #b10000))
    ((z-indexed-x) (inexact->exact #b10100))
    ((abs-y) (inexact->exact #b11000))
    ((abs-x) (inexact->exact #b11100))
    (else (error "Invalid arithmetical indexing operation")))
    (computed-opcode (bitwise-or <opcode> (bitwise-or computed-subop 1)))))
    (list (bytevector 1 computed-opcode)
    (bytevector 1 value))))))) -------------------------------------------------

    The error I am getting is as follows:

    -------------------------------------------------
    scheme@(guile-user)> (define-opcode and 1 arith)
    While compiling expression:
    Syntax error:
    unknown file:5661:0: source expression failed to match any pattern in form (let* ((computed-subop (case (subop) ((z-indirect-x) (inexact->exact 0)) ((z-page) (inexact->exact 4)) ((abs) (inexact->exact 12)) ((z-indirect-y) (inexact->exact 16)) ((z-indexed-
    x) (inexact->exact 20)) ((abs-y) (inexact->exact 24)) ((abs-x) (inexact->exact 28)) (else (error "Invalid arithmetical indexing operation"))) (computed-opcode (bitwise-or 1 (bitwise-or computed-subop 1))))) (list (bytevector 1 computed-opcode) (
    bytevector 1 value)))
    -------------------------------------------------

    If history is any guide, I may well solve this moments after posting this, but we'll see. I am sure I am overlooking something fairly basic, but what that would be is a mystery to me.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From alicetrillianosako@gmail.com@21:1/5 to All on Thu Oct 1 18:00:24 2020
    I forgot to explain the (inexact->exact) function calls. For some reason I am not entirely clear on, in the (case) conditionals, Guile kept interpreting the binary literals as vector literals, and returned an error; using the numeric 'conversion' was a
    work-around. I expect that this was also down to a misunderstanding on my part, but if anyone could provide a way to avoid this kludge, or even merely explain the error I was getting, I would be grateful.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Andrew Gierth@21:1/5 to All on Fri Oct 2 17:03:47 2020
    "alicetril" == alicetril @gmail com <alicetrillianosako@gmail.com> writes:

    alicetril> Macro definition in Scheme (Guile specifically) continues to
    alicetril> confound me, and I am hoping that in posting this, I will
    alicetril> get the nudge I need to fix my latest issue.
    [...]
    alicetril> While one of these macros is finally working, the one I've
    alicetril> been working on currently compiles as itself but returns an
    alicetril> error message when I invoke it. Here is a self-contained,
    alicetril> compilable sub-set of the code:

    It's just a misplaced paren: this line needs one more close-paren,

    alicetril> (else (error "Invalid arithmetical indexing operation")))

    and this one needs one fewer:

    alicetril> (computed-opcode (bitwise-or <opcode> (bitwise-or computed-subop 1)))))

    You'd ended up with (let* ([foo (blah) (blah)]) ...) which is not a
    valid form for let*, hence the error.

    --
    Andrew.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From alicetrillianosako@gmail.com@21:1/5 to Andrew Gierth on Fri Oct 2 10:41:13 2020
    On Friday, October 2, 2020 at 12:03:52 PM UTC-4, Andrew Gierth wrote:
    "alicetril" == alicetril @gmail com <alicetril...@gmail.com> writes:

    alicetril> Here is a self-contained,
    alicetril> compilable sub-set of the code: [...]

    It's just a misplaced paren: this line needs one more close-paren,

    *facepalm* OK, that is simply embarrassing; I guess I should have looked more carefully.. Thank you.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)