Re: [racket-users] Help debugging a custom language read/expansion error

2018-12-30 Thread Matthew Butterick


> On Dec 30, 2018, at 8:41 PM, Jonathan Simpson  wrote:
> 
> I'm doing #1, so I think #2 is the explanation. 'type' is a function 
> available in expander.rkt so the literal will only match that binding. The 
> same is true for 'offset'. Both need to be provided. 'test' is not bound to 
> anything because the macro uses 'compare' as the equivalent function, so 
> 'compare' doesn't need to be provided.
> 
> So I think it all makes sense. Thank you.


PS. This behavior with literals in syntax patterns is specific to 
`syntax-rules` and `syntax-case`. If you switch to `syntax-parse`, you can use 
its `#:datum-literals` option or its `~datum` pattern form to match raw datums 
without bindings.

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] Help debugging a custom language read/expansion error

2018-12-30 Thread Jonathan Simpson


On Sunday, December 30, 2018 at 8:51:25 PM UTC-5, Matthew Butterick wrote:
>
> Without seeing "reader.rkt" or "expander.rkt" — I'd guess that:
>
> 1) `read-syntax` isn't stripping all bindings from its parse tree before 
> returning it. Though this isn't mandatory, it's a wise practice to avoid 
> mysterious binding problems like this one. (Use `strip-context` in 
> `syntax/strip-context`.)
>
> and 
>
> 2) your list of literals in the `line` macro is stricter than you intend. 
> Macro literals match by datum AND binding, not merely by datum. So if you 
> say `(offset type test message)`, and you have `type` defined outside the 
> macro, then the literal will only match uses of that particular binding of 
> `type`. This is why `(provide type)` seems to fix the problem — you're 
> injecting the binding the macro pattern wants into the source environment. 
> Again, though Racket won't forbid you from managing bindings this way, it 
> can become a tangled web.
>


I'm doing #1, so I think #2 is the explanation. 'type' is a function 
available in expander.rkt so the literal will only match that binding. The 
same is true for 'offset'. Both need to be provided. 'test' is not bound to 
anything because the macro uses 'compare' as the equivalent function, so 
'compare' doesn't need to be provided.

So I think it all makes sense. Thank you.

-- Jonathan

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] Help debugging a custom language read/expansion error

2018-12-30 Thread Matthew Butterick
> On Dec 30, 2018, at 3:13 PM, Jonathan Simpson  wrote:
> 
> Adding a '(provide type)' in expander.rkt (where 'line' is defined) gets me 
> past this error. Strangely, providing 'compare' or 'offset' doesn't seem to 
> be necessary. I'm interested in any theories as to why this might be the case.

Without seeing "reader.rkt" or "expander.rkt" — I'd guess that:

1) `read-syntax` isn't stripping all bindings from its parse tree before 
returning it. Though this isn't mandatory, it's a wise practice to avoid 
mysterious binding problems like this one. (Use `strip-context` in 
`syntax/strip-context`.)

and 

2) your list of literals in the `line` macro is stricter than you intend. Macro 
literals match by datum AND binding, not merely by datum. So if you say 
`(offset type test message)`, and you have `type` defined outside the macro, 
then the literal will only match uses of that particular binding of `type`. 
This is why `(provide type)` seems to fix the problem — you're injecting the 
binding the macro pattern wants into the source environment. Again, though 
Racket won't forbid you from managing bindings this way, it can become a 
tangled web.

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] Help debugging a custom language read/expansion error

2018-12-30 Thread Jonathan Simpson
Adding a '(provide type)' in expander.rkt (where 'line' is defined) gets me 
past this error. Strangely, providing 'compare' or 'offset' doesn't seem to 
be necessary. I'm interested in any theories as to why this might be the 
case.

Thanks for your help.

-- Jonathan

On Sunday, December 30, 2018 at 5:14:38 PM UTC-5, Jens Axel Søgaard wrote:
>
> Den søn. 30. dec. 2018 kl. 22.33 skrev Jonathan Simpson  >:
>
>> On Sunday, December 30, 2018 at 3:58:50 PM UTC-5, Jens Axel Søgaard wrote:
>>
>>> The error
>>>
>>> line: bad syntax
>>> (line (offset 0) (type (string8 "string")) (test (strtest "MZ")))
>>>
>>> means that the macro `line` doesn't have a clause that matches the input.
>>>
>>> How is line defined?
>>>
>>> /Jens Axel
>>>
>>
>> (define-syntax line
>>   (syntax-rules (offset type test message)
>> [(line (offset off) (type type-expr) (test test-expr)) 
>>  (magic-test (offset off) (type (quote type-expr) (quote test-expr)) 
>> (compare (quote test-expr)))]
>> [(line (offset off) (type type-expr) (test test-expr) (message msg)) 
>>  (magic-test (offset off) (type (quote type-expr) (quote test-expr)) 
>> (compare (quote test-expr)) msg)]))
>>
>> This is defined in my expander, and it seems to work from the REPL with 
>> expander.rkt loaded.
>>
>
> Try adding a clause:
>  
> (define-syntax line
>   (syntax-rules (offset type test message)
> [(line (offset off) (type type-expr) (test test-expr)) 
>  (magic-test (offset off) (type (quote type-expr) (quote test-expr)) 
> (compare (quote test-expr)))]
> [(line (offset off) (type type-expr) (test test-expr) (message msg)) 
>  (magic-test (offset off) (type (quote type-expr) (quote test-expr)) 
> (compare (quote test-expr)) msg)]
> [_ "no clause found in line"]))
>
> If you get the string instead of the "bad syntax" we know that something 
> unexpected happened.
>
> I am guessing it has "something" to do with either offset, type, test or 
> message which
> will only match in syntax-rules, if the context is correct.
>
> /Jens Axel
>
>
>  
>

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] Help debugging a custom language read/expansion error

2018-12-30 Thread Jens Axel Søgaard
Den søn. 30. dec. 2018 kl. 22.33 skrev Jonathan Simpson :

> On Sunday, December 30, 2018 at 3:58:50 PM UTC-5, Jens Axel Søgaard wrote:
>
>> The error
>>
>> line: bad syntax
>> (line (offset 0) (type (string8 "string")) (test (strtest "MZ")))
>>
>> means that the macro `line` doesn't have a clause that matches the input.
>>
>> How is line defined?
>>
>> /Jens Axel
>>
>
> (define-syntax line
>   (syntax-rules (offset type test message)
> [(line (offset off) (type type-expr) (test test-expr))
>  (magic-test (offset off) (type (quote type-expr) (quote test-expr))
> (compare (quote test-expr)))]
> [(line (offset off) (type type-expr) (test test-expr) (message msg))
>  (magic-test (offset off) (type (quote type-expr) (quote test-expr))
> (compare (quote test-expr)) msg)]))
>
> This is defined in my expander, and it seems to work from the REPL with
> expander.rkt loaded.
>

Try adding a clause:

(define-syntax line
  (syntax-rules (offset type test message)
[(line (offset off) (type type-expr) (test test-expr))
 (magic-test (offset off) (type (quote type-expr) (quote test-expr))
(compare (quote test-expr)))]
[(line (offset off) (type type-expr) (test test-expr) (message msg))
 (magic-test (offset off) (type (quote type-expr) (quote test-expr))
(compare (quote test-expr)) msg)]
[_ "no clause found in line"]))

If you get the string instead of the "bad syntax" we know that something
unexpected happened.

I am guessing it has "something" to do with either offset, type, test or
message which
will only match in syntax-rules, if the context is correct.

/Jens Axel

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] Help debugging a custom language read/expansion error

2018-12-30 Thread Jonathan Simpson
On Sunday, December 30, 2018 at 3:58:50 PM UTC-5, Jens Axel Søgaard wrote:

> The error
>
> line: bad syntax
> (line (offset 0) (type (string8 "string")) (test (strtest "MZ")))
>
> means that the macro `line` doesn't have a clause that matches the input.
>
> How is line defined?
>
> /Jens Axel
>

(define-syntax line
  (syntax-rules (offset type test message)
[(line (offset off) (type type-expr) (test test-expr)) 
 (magic-test (offset off) (type (quote type-expr) (quote test-expr)) 
(compare (quote test-expr)))]
[(line (offset off) (type type-expr) (test test-expr) (message msg)) 
 (magic-test (offset off) (type (quote type-expr) (quote test-expr)) 
(compare (quote test-expr)) msg)]))

This is defined in my expander, and it seems to work from the REPL with 
expander.rkt loaded.

-- Jonathan
 

>  
>
> Den søn. 30. dec. 2018 kl. 21.27 skrev Jonathan Simpson  >:
>
>> I'm bumbling through my first attempts at creating a language in Racket 
>> and am currently stuck debugging an error in expansion. I have a macro in 
>> my expander that works fine until I try to run the language.
>>
>> Here's what the macro stepper in DrRacket gives me:
>>  
>> [Error]
>>
>>
>> line: bad syntax
>> (line (offset 0) (type (string8 "string")) (test (strtest "MZ")))
>>
>>
>> while executing macro transformer in:
>> (module magic-mod "expander.rkt"
>>   (#%module-begin
>>(module configure-runtime '#%kernel
>>  (#%module-begin (#%require racket/runtime-config) (#%app configure '
>> #f)))
>>(#%app call-with-values (lambda () (quote #f)) print-values)
>>(#%app call-with-values (lambda () (quote #f)) print-values)
>>(#%app
>> call-with-values
>> (lambda ()
>>   (#%app
>>(line (offset 0) (type (string8 "string")) (test (strtest "MZ")))
>>(level
>> (line
>>  (offset 24)
>>  (type (numeric "leshort"))
>>  (test (numtest "<" 64))
>>  (message "MZ executable (MS-DOS)"))
>> (line (offset 24) (type (numeric "leshort")) (test (numtest ">" 
>> 63)))
>> (level
>>  (line
>>   (offset (indoff 60 (size (lelong ".l"
>>   (type (string8 "string"))
>>   (test (strtest "PE\\0\\0"))
>>   (message "PE executable (MS-Windows)"))
>>  (line
>>   (offset (indoff 60 (size (lelong ".l"
>>   (type (string8 "string"))
>>   (test (strtest "LX\\0\\0"))
>>   (message "LX executable (OS/2)"))
>> print-values)))
>>
>>
>>
>> The macro line should expand to:
>>
>> (magic-test (offset 0) (type '(string8 "string") '(strtest "MZ")) (compare 
>> '(strtest "MZ")))
>>
>> And this works fine when run it from my expander.rkt.
>>
>> Here's the file I'm actually executing when I get the error:
>> #lang reader "reader.rkt"
>>
>>
>> # MS Windows executables are also valid MS-DOS executables
>> 0   string  MZ
>> >0x18   leshort <0x40   MZ executable (MS-DOS)
>> #>>(4.s*512) leshort 0x014c  COFF executable (MS-DOS, DJGPP)
>> #>>(4.s*512) leshort !0x014c MZ executable (MS-DOS)
>> # skip the whole block below if it is not an extended executable
>> >0x18   leshort >0x3f
>> >>(0x3c.l)  string  PE\0\0  PE executable (MS-Windows)
>> >>(0x3c.l)  string  LX\0\0  LX executable (OS/2)
>>
>> I know that the procedure application will eventually fail because 
>> magic-test doesn't return a function. This code is a work in progress. I 
>> just want to know if the reading/expansion is working properly. I feel like 
>> I'm probably just missing something basic.
>>
>> Thanks for any help,
>> -- Jonathan
>>
>> -- 
>> You received this message because you are subscribed to the Google Groups 
>> "Racket Users" group.
>> To unsubscribe from this group and stop receiving emails from it, send an 
>> email to racket-users...@googlegroups.com .
>> For more options, visit https://groups.google.com/d/optout.
>>
>
>
> -- 
> -- 
> Jens Axel Søgaard
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] Help debugging a custom language read/expansion error

2018-12-30 Thread Jens Axel Søgaard
The error

line: bad syntax
(line (offset 0) (type (string8 "string")) (test (strtest "MZ")))

means that the macro `line` doesn't have a clause that matches the input.

How is line defined?

/Jens Axel


Den søn. 30. dec. 2018 kl. 21.27 skrev Jonathan Simpson :

> I'm bumbling through my first attempts at creating a language in Racket
> and am currently stuck debugging an error in expansion. I have a macro in
> my expander that works fine until I try to run the language.
>
> Here's what the macro stepper in DrRacket gives me:
>
> [Error]
>
>
> line: bad syntax
> (line (offset 0) (type (string8 "string")) (test (strtest "MZ")))
>
>
> while executing macro transformer in:
> (module magic-mod "expander.rkt"
>   (#%module-begin
>(module configure-runtime '#%kernel
>  (#%module-begin (#%require racket/runtime-config) (#%app configure '
> #f)))
>(#%app call-with-values (lambda () (quote #f)) print-values)
>(#%app call-with-values (lambda () (quote #f)) print-values)
>(#%app
> call-with-values
> (lambda ()
>   (#%app
>(line (offset 0) (type (string8 "string")) (test (strtest "MZ")))
>(level
> (line
>  (offset 24)
>  (type (numeric "leshort"))
>  (test (numtest "<" 64))
>  (message "MZ executable (MS-DOS)"))
> (line (offset 24) (type (numeric "leshort")) (test (numtest ">" 63
> )))
> (level
>  (line
>   (offset (indoff 60 (size (lelong ".l"
>   (type (string8 "string"))
>   (test (strtest "PE\\0\\0"))
>   (message "PE executable (MS-Windows)"))
>  (line
>   (offset (indoff 60 (size (lelong ".l"
>   (type (string8 "string"))
>   (test (strtest "LX\\0\\0"))
>   (message "LX executable (OS/2)"))
> print-values)))
>
>
>
> The macro line should expand to:
>
> (magic-test (offset 0) (type '(string8 "string") '(strtest "MZ")) (compare
> '(strtest "MZ")))
>
> And this works fine when run it from my expander.rkt.
>
> Here's the file I'm actually executing when I get the error:
> #lang reader "reader.rkt"
>
>
> # MS Windows executables are also valid MS-DOS executables
> 0   string  MZ
> >0x18   leshort <0x40   MZ executable (MS-DOS)
> #>>(4.s*512) leshort 0x014c  COFF executable (MS-DOS, DJGPP)
> #>>(4.s*512) leshort !0x014c MZ executable (MS-DOS)
> # skip the whole block below if it is not an extended executable
> >0x18   leshort >0x3f
> >>(0x3c.l)  string  PE\0\0  PE executable (MS-Windows)
> >>(0x3c.l)  string  LX\0\0  LX executable (OS/2)
>
> I know that the procedure application will eventually fail because
> magic-test doesn't return a function. This code is a work in progress. I
> just want to know if the reading/expansion is working properly. I feel like
> I'm probably just missing something basic.
>
> Thanks for any help,
> -- Jonathan
>
> --
> You received this message because you are subscribed to the Google Groups
> "Racket Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to racket-users+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>


-- 
-- 
Jens Axel Søgaard

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[racket-users] Help debugging a custom language read/expansion error

2018-12-30 Thread Jonathan Simpson
I'm bumbling through my first attempts at creating a language in Racket and 
am currently stuck debugging an error in expansion. I have a macro in my 
expander that works fine until I try to run the language.

Here's what the macro stepper in DrRacket gives me:
 
[Error]


line: bad syntax
(line (offset 0) (type (string8 "string")) (test (strtest "MZ")))


while executing macro transformer in:
(module magic-mod "expander.rkt"
  (#%module-begin
   (module configure-runtime '#%kernel
 (#%module-begin (#%require racket/runtime-config) (#%app configure '
#f)))
   (#%app call-with-values (lambda () (quote #f)) print-values)
   (#%app call-with-values (lambda () (quote #f)) print-values)
   (#%app
call-with-values
(lambda ()
  (#%app
   (line (offset 0) (type (string8 "string")) (test (strtest "MZ")))
   (level
(line
 (offset 24)
 (type (numeric "leshort"))
 (test (numtest "<" 64))
 (message "MZ executable (MS-DOS)"))
(line (offset 24) (type (numeric "leshort")) (test (numtest ">" 63
)))
(level
 (line
  (offset (indoff 60 (size (lelong ".l"
  (type (string8 "string"))
  (test (strtest "PE\\0\\0"))
  (message "PE executable (MS-Windows)"))
 (line
  (offset (indoff 60 (size (lelong ".l"
  (type (string8 "string"))
  (test (strtest "LX\\0\\0"))
  (message "LX executable (OS/2)"))
print-values)))



The macro line should expand to:

(magic-test (offset 0) (type '(string8 "string") '(strtest "MZ")) (compare 
'(strtest 
"MZ")))

And this works fine when run it from my expander.rkt.

Here's the file I'm actually executing when I get the error:
#lang reader "reader.rkt"


# MS Windows executables are also valid MS-DOS executables
0   string  MZ
>0x18   leshort <0x40   MZ executable (MS-DOS)
#>>(4.s*512) leshort 0x014c  COFF executable (MS-DOS, DJGPP)
#>>(4.s*512) leshort !0x014c MZ executable (MS-DOS)
# skip the whole block below if it is not an extended executable
>0x18   leshort >0x3f
>>(0x3c.l)  string  PE\0\0  PE executable (MS-Windows)
>>(0x3c.l)  string  LX\0\0  LX executable (OS/2)

I know that the procedure application will eventually fail because 
magic-test doesn't return a function. This code is a work in progress. I 
just want to know if the reading/expansion is working properly. I feel like 
I'm probably just missing something basic.

Thanks for any help,
-- Jonathan

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.