andrew-johnson-4 / lambda-mountain

Compiler Backend for LSTS (Typed Macro Assembler)
https://andrew-johnson-4.github.io/lambda-mountain/
MIT License
20 stars 1 forks source link

[help wanted] `Invalid Macro Application` error #952

Open alex-s168 opened 6 days ago

alex-s168 commented 6 days ago

I was trying to implement a (not really efficient) umbra string type like this:

type Umbra (Umbra(
  len: U32 ,
  backing: (UmbraShort(arr: Array<U8, 12>))
         | (UmbraLong(prefix: Array<U8, 4> , ptr: U8[]))
));
...
addr := λ(: s Umbra). (: (
   (match (.backing s) (
      ( (UmbraShort( arr ))
           (as (& arr) U8[]) )
      ( (UmbraLong( _ ptr )) 
           ptr )
   ))
) U8[]);
...

but I get this error and I don't know why:

Invalid Macro Application

In File PLATFORM/C/LIB/umbra.lm Line 63 Column 5
In Function Application
match-pats:
?
((uuid_000000000000584c ('UmbraShort arr)) (scope (((if (match-pats-arm (uuid_000000000000584c (as (& arr))))) 'U8[]) (scope (((if (match-pats-arm (uuid_000000000000584c ('UmbraLong (_ ptr))))) ptr) (fail ('Pattern\sMatch\sFailure_s (': ((p ':Location:) Constant + Literal + String))))))))):
?
Return:
?
andrew-johnson-4 commented 6 days ago

Yeah, that error message sucks. LM doesn't support inline compound typedefs or untagged unions yet.

type UmbraShortLong (UmbraShort( arr:Array<U8,12> )) | (UmbraLong(prefix: Array<U8, 4> , ptr: U8[]));
type Umbra (Umbra( len:U32, backing:UmbraShortLong ));

or

type Umbra (UmbraShort( len:U32, arr:Array<U8,12> )) | (UmbraLong( len:U32, prefix: Array<U8, 4>, ptr: U8[]));
alex-s168 commented 6 days ago

I'm getting a really weird error: Pattern Match Failure at File: PLUGINS/BACKEND/C/compile-expr-direct.lm Line: 89 Column: 14

Source

andrew-johnson-4 commented 6 days ago

Hmm, this is a nasty error. LM does not excel at optimizing custom data structures atm. Especially when interacting with the "match" destructuring primitive.

Match currently tries to dereference pointers by default to permit further destructuring or to create useful values, which is not at all what you are trying to do.

I am going to

andrew-johnson-4 commented 6 days ago

field accessors directly still work (.prefix u) if you want to destructure manually. You can also avoid this error in match by not binding anything (UmbraLong( _ ))

However, I would also suggest sitting on this for a while, as what you are trying to do will be better supported in the near future. The C backend is literally about 2 weeks old.

andrew-johnson-4 commented 6 days ago

Your code is very idiomatic. Thanks for taking the time to study so much.

andrew-johnson-4 commented 6 days ago

a lot of good ideas coming from this