Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Activity
  • 18:09

    bakpakin on master

    Address windows compilation war… (compare)

  • 17:45

    bakpakin on master

    MAP_ANON(YMOUS) not strictly ne… (compare)

  • 17:30

    bakpakin on master

    Add MAP_ANON instead of MAP_ANO… (compare)

  • 17:26

    bakpakin on master

    Add ffi/jitfn for JIT compilati… (compare)

  • Nov 27 16:14

    bakpakin on master

    Format compile.c (compare)

  • Nov 25 20:19
    pixelherodev edited #1047
  • Nov 25 20:17
    pixelherodev opened #1047
  • Nov 21 05:00
    dressupgeekout opened #1046
  • Nov 16 12:54
    tionis synchronize #166
  • Nov 15 03:27
    Pebaz closed #1044
  • Nov 15 03:27
    Pebaz commented #1044
  • Nov 14 08:04
    sogaiu commented #1044
  • Nov 14 02:09
    sogaiu commented #1044
  • Nov 13 20:57
    Pebaz edited #1044
  • Nov 13 20:56
    Pebaz opened #1044
  • Nov 11 17:48

    bakpakin on master

    Update state header to include … (compare)

  • Nov 11 17:25

    bakpakin on master

    Simplify eval. Also add more c… (compare)

  • Nov 11 17:15

    bakpakin on master

    Simplify eval-string implementa… (compare)

  • Nov 11 17:02

    bakpakin on master

    Create pthread threads with det… (compare)

  • Nov 10 22:57
    bakpakin opened #1043
petrak@
@petrakat:matrix.org
[m]
man i'm proud of this scrappy little language. it's the first lang i ever wrote and it's a hot mess but i'm proud dammit
petrak@
@petrakat:matrix.org
[m]
(defmacro delim [main sep]
  ~'(*
      (any (* ,main ,sep))
      ,main))
woohoo this seems to work
whoops, actually
(defmacro delim [main sep]
  ~'(*
      ,main
      (any (* ,sep ,main))))
this doesn't crash and burn if the input ends with the delim
willfennel
@willfennel:matrix.org
[m]

Messing around with PEGs a bit as well.

This should be the same as what you're doing, petrak@ ?

(def grammar
  (peg/compile
    ~{:main (sequence :num (any (sequence "," :num)))
      :num (cmt (capture :d+) ,scan-number)}))
(peg/match grammar "111,222,333")
# => @[111 222 333]
willfennel
@willfennel:matrix.org
[m]

I made a PEG to parse the input for AoC 2022 day 1: https://paste.sr.ht/~harryvederci/6bb9f12eb3664c1e2a9cf68330e75066b38698c3

Is there a better way to do this? It's definitely a steep learning curve, but it feels like it may unlock a new part of my brain with unlimited potential 😁

(Not really a spoiler but I thought I'd put it in a paste instead of putting the code here, just in case.)

15 replies
debris
@vertices:matrix.org
[m]
Hey all. Could Janet call a function / jump execution to a label in assembly the same way it can call cfunctions?
1 reply
dressupgeekout
@dressupgeekout:matrix.org
[m]

If I wanted to achieve something like that, I'd attempt to write a cfunction which in turn would do all that messy asm stuff.

Idk why I'd want or need that, but, just a thought

dressupgeekout
@dressupgeekout:matrix.org
[m]
(I also halfway don't know what I'm talking about heh)
bakpakin
@bakpakin:matrix.org
[m]
Not sure what you are trying to do, are you trying to do something like insert x64 assembly into a Janet function?
if so, no, not like that
there is an FFI, and Janet has bytecode assembly
that you can insert "jump" instructions in
although you can't mix assembly into a normal Janet function
there is just (asm ...) and (disasm closure)
that would be interesting though, extending asm to be able to compile some machine native assembly to c functions...
1 reply
sogaiu
@sogaiu:matrix.org
[m]
willfennel: on a side note, are you aware of the number peg special? https://github.com/sogaiu/margaret/blob/master/examples/number.janet
1 reply
willfennel
@willfennel:matrix.org
[m]
Nice, @pixelherodev !
1 reply
willfennel
@willfennel:matrix.org
[m]
Is there a way to step through the execution of peg/match to see what is happening exactly?
tionis
@tionis:matrix.org
[m]
You could use the debugging from https://github.com/sogaiu/margaret
1 reply
willfennel
@willfennel:matrix.org
[m]
Yeah that works:
(def input-file "01-input.txt")
(def elves
  (-> (peg/match
        ~{:line (sequence
                  (number :d+)
                  "\n")
          :elf  (sequence
                  (cmt (some :line) ,+)
                  (? "\n"))
          :main (some :elf)}
        (file/read (file/open input-file :rn) :all))
      (sort >)))
(def p1 (get elves 0))
(def p2 (+ ;(array/slice elves 0 3)))
Must be amusing to bakpakin, sogaiu, etc to see us going "omg this new PEG thing I just discovered is amazing!" 😄
pixelherodev
@pixelherodev
I've been using PEG for a while via minipeg (https://ach.srht.site/minipeg), but janet's PEG module is a lot nicer IMO :)
pixelherodev
@pixelherodev
My answer for AoC day 2 is a bit grosser than day 1 :P https://pixelhero.dev/aoc/2022/2.janet (spoilers (duh :))
9 replies
willfennel
@willfennel:matrix.org
[m]
@pixelherodev: instead of (+ ;(map ...)) you can do (sum (map ...)), I think
I guess it's longer, though lol
pixelherodev
@pixelherodev
ha, yeah :P
pixelherodev
@pixelherodev
That's... hm. I wonder which is better? Sum is implemented (tail +639 src/janet/src/boot/boot.janet | read -n5) as creating a variable, and (each x xs (+= accum x)). + is an SSI instruction, I believe, and splice is a special form, so I dunno how it work; I haven't looked into the internals yet.
pixelherodev
@pixelherodev
Huh. Looks like splicing basically just adds a reference to the array, and a flag marking it as spliced? So, basically, (+ ;foo) maps down to a single IR instruction with a single op with a flag set. (sum foo), on the other hand, compiles down to (var s 0) (each x foo (set foo (+ foo x))) s. If I'm understanding that right, shouldn't (+ ;foo) actually be faster?
Even discounting the fact that + doesn't require a call/return IIUC, and assuming that the fetch is optimized away (I dunno that it is; I haven't looked at the bytecode interpreter yet), that still means time spent optimizing it away, which isn't needed for the (+ ;foo) case - and if it's not optimized away, that means that the interpreter has to run an additional fetch per argument (which is going to hit cache, but still.)
...of course this is all baseless speculation, so /shrug
willfennel
@willfennel:matrix.org
[m]
Time for some benchmarking? 😄
I guess if one would be objectively faster than the other, the other could have just been a syntactic sugar version of the faster one.
sogaiu
@sogaiu:matrix.org
[m]
@pixelherodev: re: documentation of number - i don't see it listed at janet-lang.org, but i see it here: https://github.com/janet-lang/janet/blob/master/CHANGELOG.md#1180---2021-10-10
perhaps a pr would be welcome here: https://github.com/janet-lang/janet-lang.org/

Is this janet syntax PEG definition on the website correct?
I'm still new at PEGs but I notice the following:

(peg/match grammar "(+ 1 1 2)")
# =>  @[1 1 2]
(peg/match grammar "(/ 1 1 2)")
# =>  @[1 1 2]
(peg/match grammar "[:a 1 2]")
# =>  @[1 2]
(peg/match grammar "(defn myplus [x] (+ x x))")
# =>  @[]

I guess technically these don't return nil so they are correct, but I thought they would return @[] in all cases.

sogaiu
@sogaiu:matrix.org
[m]

willfennel: it may be that the <- part of: :number (cmt (<- :token) ,scan-number) is capturing as <- is an alias for capture.

i think this grammar was put together before the number peg special.

one might be able to write the :number bit as :number (number :token) these days. if it's important not to retain the captured numbers then perhaps :number (drop (number :token)) is appropriate as number is a capturing peg special.

willfennel
@willfennel:matrix.org
[m]
Ah thanks, I'll give that a try soon
sogaiu
@sogaiu:matrix.org
[m]
on a side note, there are some other pegs for parsing janet too. here are some example usages to show the kinds of parse results one can get: https://github.com/sogaiu/janet-peg/blob/master/janet-peg/rewrite.janet#L27-L71 -- one can even do round-tripping: https://github.com/sogaiu/janet-peg/blob/master/janet-peg/rewrite.janet#L255-L259
1 reply
debris
@vertices:matrix.org
[m]
I am asking that because I have made/am working on a program that can produce nasm assembly code from a janet array of arrays. It's a very simple concept but it gets awfully powerful the moment you realize you can manipulate that array however way you wish prior to , I guess, transpiling it to assembly.
I don't have a janet assembler handy so I offload the work to the nasm binary, but thus far it's been very successful, at least as successful as it can be with my limited assembly knowledge.
debris
@vertices:matrix.org
[m]
Maybe if machine code could be loaded into memory and ran, Janet could write assembly code at runtime - which since those are just array manipulations, would be particularly easy - and achieve some kind of JIT assembling (rather than compiling) capabilities; like a buff version of C's asm keyword.
bakpakin
@bakpakin:matrix.org
[m]
Yes, that should work fine. The east part is calling mmap and mprotect to get executable memory. Then you could set it up so you would call it as a cfunction or use the FFI functions to call it. The hard part is the assembler
1 reply
petrak@
@petrakat:matrix.org
[m]
Does janet have character literals?
debris
@vertices:matrix.org
[m]
I don't think so , not by defaullt; you're supposed to use either a number corresponding to the char or a length one string.
At least, map seems to behave as if strings were tuples of char-sized numbers: