Yuescript icon indicating copy to clipboard operation
Yuescript copied to clipboard

An Overview of YueScript failed to run

Open m3makefile opened this issue 8 months ago • 3 comments

The sample code from https://yuescript.org/doc/#an-overview-of-yuescript:

-- import syntax
import "yue" as :p, :to_lua

-- object literals
inventory =
  equipment:
    * "sword"
    * "shield"
  items:
    * name: "potion"
      count: 10
    * name: "bread"
      count: 3

-- pipe operator
[1, 2, 3]
  |> map (x) -> x * 2
  |> filter (x) -> x > 4
  |> reduce 0, (a, b) -> a + b
  |> print

-- metatable manipulation
apple =
  size: 15
  <index>:
    color: 0x00ffff

with apple
  p .size, .color, .<index> if .<>?

-- js-like export syntax
export 🌛 = "月之脚本"

yue -e test.yue

test.yue:16: attempt to call a nil value (global 'map')
Stack Traceback
===============
(1) global C function 'xpcall'
(2) '=(yuescript)':110


m3makefile avatar Mar 09 '25 11:03 m3makefile

The issue arises because the sample code is meant to showcase the syntax of Yuescript, not to serve as a fully functional example. To make it runnable, you'll need to define the missing implementations for map, filter, and reduce. Here’s how you can add them:

map = (arr, f) -> [f item for item in *arr]
filter = (arr, f) -> [item for item in *arr when f item]
reduce = (arr, def, f) ->
  if #arr == 0
    def
  else
    res = def
    for item in *arr
      res = f res, item
    res

These functions will allow the pipe operator example in the code to work as intended. However, please note that the primary purpose of the sample is to demonstrate Yuescript's syntax rather than provide a complete tutorial or fully executable script.

pigpigyyy avatar Mar 10 '25 01:03 pigpigyyy

I want to test if the interpreter works. I try to find sample codes but I can't find anything. There is almost no information available. The document on your website doesn't even tell me what is the file extension for YueScript source file. I know it's .yue thank to the code editor I'm using has built-in syntax highlighting support for both MoonScript and YueScript.

m3makefile avatar Mar 10 '25 05:03 m3makefile

OK, the .yue extention should get mentioned in the docs. And I'll consider making this overview sample to be runnable.

pigpigyyy avatar Mar 10 '25 09:03 pigpigyyy