mildc055ee
2021-11-17 09:54:41

what is the scheme (or lisp? idk) way to represent C enum like typedef enum WASMOpcode { WASM_OP_UNREACHABLE = 0x00, WASM_OP_NOP = 0x01, ... } (from https://github.com/bytecodealliance/wasm-micro-runtime/blob/main/core/iwasm/interpreter/wasm_opcode.h)


sorawee
2021-11-17 09:57:27

IIUC, people usually use symbols instead. E.g., 'WASM-OP-UNREACHABLE


sorawee
2021-11-17 09:59:00

And you can define (define WASMOpcode/c (or/c 'WASM-OP-UNREACHABLE 'WASM_OP_NOP ...)) to be a contract to recognize these values


sorawee
2021-11-17 10:01:13

If you are willing to go beyond standard Racket libraries, some user libraries have the enum concept that you can use. E.g., https://docs.racket-lang.org/rebellion/Enum_Types.html


mildc055ee
2021-11-17 10:07:01

Ah, I see. so the parser should be like (w/o libraries) (match byte [#x00 'WASM-OP-UNREACHABLE] [#x01 'WASM-OP-NOP] ...) Can I use this style in typed racket too? or does any better way exist?


sorawee
2021-11-17 10:13:42

Oh, so you are dealing with external data with those enum values…


sorawee
2021-11-17 10:14:17

I mean, if you want to convert these external data into stuff like 'WASM-OP-UNREACHABLE, that’s totally possible, and definitely can be done in #lang typed/racket


sorawee
2021-11-17 10:14:34

Something like:

#lang typed/racket (define-type WASMOpcode (U 'WASM-OP-UNREACHABLE 'WASM-OP-NOP)) (: test : Number -> WASMOpcode) (define (test x) (match x [0 'WASM-OP-UNREACHABLE] [1 'WASM-OP-NOP]))


sorawee
2021-11-17 10:15:44

But if you don’t want to parse, I guess you could also do:

(define WASM-OP-UNREACHABLE 0) (define WASM-OP-NOP 1) and use these identifiers?


sorawee
2021-11-17 10:16:33

If you don’t want to write consecutive numbers 0, 1, 2, …, you can also use macros to generate these numbers automatically.


soegaard2
2021-11-17 10:39:17

If the context is writing a C API, then use _enum. Otherwise, just use symbols.

https://docs.racket-lang.org/foreign/Enumerations_and_Masks.html#%28def._%28%28lib._ffi%2Funsafe..rkt%29.__enum%29%29


raoul.schorer
2021-11-17 20:44:28

Hi, how can I make a syntax-parse macro that would expand to a module that I could write to a file without the calling code needing to (require file/module) but only (require file), i.e. the macro expansion would be “spliced” inside the file?


samth
2021-11-17 20:47:25

Can you make your question a little more concrete, perhaps with an example of preciesely what you want to write?


raoul.schorer
2021-11-17 21:09:32

This (define-syntax (i-expand-in-a-module stx) (syntax-parse stx [(_ s) #'(module simplest-module-ever racket s)])) (with-output-to-file path (lambda () (write (i-expand-in-a-module (list 'a 'b 'c)))) expands to (module simplest-module-ever racket ;whatever 's' is) in file.rkt. Then, if I want to require this module I’d have to do (require file/simplest-module-ever), isn’t it? What I’d like is to write (require file) and have simplest-module-ever available. Is that possible?


samth
2021-11-17 21:17:23

I’m confused. That program does not do what you describe. It produces a syntax error, because you expanded to a module form inside an application, which is not where modules go.


samth
2021-11-17 21:17:48

It’s certainly possible to have macros that expand to submodules which live inside other modules.


samth
2021-11-17 21:18:14

But in general macro-expansion doesn’t create new files on disk using write.


samth
2021-11-17 21:19:45

Then there’s another question which is you’d like to be able to write (require a) and have it actually require a/b.rkt. You can certainly write a require transformer, called say f, and then you could have (require (f a)) actually mean (require a/b.rkt). Would that be ok?


raoul.schorer
2021-11-17 21:24:15

I’m confused myself. I understand the transformer bit. I’ll work some more to try to understand the source of my confusion and come back. Thank you for your time and kindness.


soegaard2
2021-11-17 21:35:05

FWIW - it’s possible to use syntax-parse in phase 0, so you don’t need to use a macro to construct your module.


raoul.schorer
2021-11-17 21:58:41

@soegaard2 would that be done by wrapping the (syntax-parse ... expression as (define (p0 arg) (syntax-parse arg ... instead of (define-syntax (p0 stx) (syntax-parse stx .. or is there some other trick to it? I can’t get it to work at phase 0 for now.


soegaard2
2021-11-17 22:21:34

Something like this: #lang racket (require syntax/parse) (define (i-expand-in-a-module stx) (syntax-parse stx [s #'(module simplest-module-ever racket s)])) (i-expand-in-a-module #'(list 'a 'b 'c))


soegaard2
2021-11-17 22:22:36

This particular example could also be written as: (define (i-expand-in-a-module s) (with-syntax ([s s]) #'(module simplest-module-ever racket s)))


soegaard2
2021-11-17 22:23:06

@raoul.schorer ^