KCL: Keyword fn args like "x = 1" not like "x: 1" (#4770)

Aligns with how we're doing objects.
This commit is contained in:
Adam Chalmers
2024-12-12 11:53:35 -06:00
committed by GitHub
parent 05163fdded
commit 191b9b71fd
21 changed files with 79 additions and 68 deletions

File diff suppressed because one or more lines are too long

View File

@ -149196,7 +149196,7 @@
"unpublished": false, "unpublished": false,
"deprecated": false, "deprecated": false,
"examples": [ "examples": [
"assertEqual(rem(7, divisor: 4), 3, 0.01, \"remainder is 3\")\nassertEqual(rem(-7, divisor: 4), -3, 0.01, \"remainder is 3\")\nassertEqual(rem(7, divisor: -4), 3, 0.01, \"remainder is 3\")" "assertEqual(rem(7, divisor = 4), 3, 0.01, \"remainder is 3\")\nassertEqual(rem(-7, divisor = 4), -3, 0.01, \"remainder is -3\")\nassertEqual(rem(7, divisor = -4), 3, 0.01, \"remainder is 3\")"
] ]
}, },
{ {

View File

@ -119,6 +119,11 @@
"title": "Pipe and Flange Assembly", "title": "Pipe and Flange Assembly",
"description": "A crucial component in various piping systems, designed to facilitate the connection, disconnection, and access to piping for inspection, cleaning, and modifications. This assembly combines pipes (long cylindrical conduits) with flanges (plate-like fittings) to create a secure yet detachable joint." "description": "A crucial component in various piping systems, designed to facilitate the connection, disconnection, and access to piping for inspection, cleaning, and modifications. This assembly combines pipes (long cylindrical conduits) with flanges (plate-like fittings) to create a secure yet detachable joint."
}, },
{
"file": "pipe-with-bend.kcl",
"title": "Pipe with bend",
"description": "A tubular section or hollow cylinder, usually but not necessarily of circular cross-section, used mainly to convey substances that can flow."
},
{ {
"file": "poopy-shoe.kcl", "file": "poopy-shoe.kcl",
"title": "Poopy Shoe", "title": "Poopy Shoe",

View File

@ -2256,12 +2256,16 @@ fn arguments(i: &mut TokenSlice) -> PResult<Vec<Expr>> {
} }
fn labeled_argument(i: &mut TokenSlice) -> PResult<LabeledArg> { fn labeled_argument(i: &mut TokenSlice) -> PResult<LabeledArg> {
separated_pair(identifier, (one_of(TokenType::Colon), opt(whitespace)), expression) separated_pair(
.map(|(label, arg)| LabeledArg { terminated(identifier, opt(whitespace)),
label: label.inner, terminated(one_of((TokenType::Operator, "=")), opt(whitespace)),
arg, expression,
}) )
.parse_next(i) .map(|(label, arg)| LabeledArg {
label: label.inner,
arg,
})
.parse_next(i)
} }
/// Arguments are passed into a function, /// Arguments are passed into a function,
@ -4057,7 +4061,7 @@ let myBox = box([0,0], -3, -16, -10)
#[test] #[test]
fn kw_fn() { fn kw_fn() {
for input in ["val = foo(x, y: z)", "val = foo(y: z)"] { for input in ["val = foo(x, y = z)", "val = foo(y = z)"] {
let module_id = ModuleId::default(); let module_id = ModuleId::default();
let tokens = crate::parsing::token::lex(input, module_id).unwrap(); let tokens = crate::parsing::token::lex(input, module_id).unwrap();
super::program.parse(tokens.as_slice()).unwrap(); super::program.parse(tokens.as_slice()).unwrap();
@ -4497,8 +4501,8 @@ my14 = 4 ^ 2 - 3 ^ 2 * 2
r#"x = 3 r#"x = 3
obj = { x, y: 4}"# obj = { x, y: 4}"#
); );
snapshot_test!(kw_function_unnamed_first, r#"val = foo(x, y: z)"#); snapshot_test!(kw_function_unnamed_first, r#"val = foo(x, y = z)"#);
snapshot_test!(kw_function_all_named, r#"val = foo(x: a, y: b)"#); snapshot_test!(kw_function_all_named, r#"val = foo(x = a, y = b)"#);
snapshot_test!(kw_function_decl_all_labeled, r#"fn foo(x, y) { return 1 }"#); snapshot_test!(kw_function_decl_all_labeled, r#"fn foo(x, y) { return 1 }"#);
snapshot_test!(kw_function_decl_first_unlabeled, r#"fn foo(@x, y) { return 1 }"#); snapshot_test!(kw_function_decl_first_unlabeled, r#"fn foo(@x, y) { return 1 }"#);
snapshot_test!(kw_function_decl_with_default_no_type, r#"fn foo(x? = 2) { return 1 }"#); snapshot_test!(kw_function_decl_with_default_no_type, r#"fn foo(x? = 2) { return 1 }"#);

View File

@ -1,12 +1,13 @@
--- ---
source: kcl/src/parsing/parser.rs source: kcl/src/parsing/parser.rs
expression: actual expression: actual
snapshot_kind: text
--- ---
{ {
"body": [ "body": [
{ {
"declaration": { "declaration": {
"end": 21, "end": 23,
"id": { "id": {
"end": 3, "end": 3,
"name": "val", "name": "val",
@ -22,9 +23,9 @@ expression: actual
"name": "x" "name": "x"
}, },
"arg": { "arg": {
"end": 14, "end": 15,
"name": "a", "name": "a",
"start": 13, "start": 14,
"type": "Identifier", "type": "Identifier",
"type": "Identifier" "type": "Identifier"
} }
@ -36,9 +37,9 @@ expression: actual
"name": "y" "name": "y"
}, },
"arg": { "arg": {
"end": 20, "end": 22,
"name": "b", "name": "b",
"start": 19, "start": 21,
"type": "Identifier", "type": "Identifier",
"type": "Identifier" "type": "Identifier"
} }
@ -50,7 +51,7 @@ expression: actual
"start": 6, "start": 6,
"type": "Identifier" "type": "Identifier"
}, },
"end": 21, "end": 23,
"start": 6, "start": 6,
"type": "CallExpressionKw", "type": "CallExpressionKw",
"type": "CallExpressionKw", "type": "CallExpressionKw",
@ -59,13 +60,13 @@ expression: actual
"start": 0, "start": 0,
"type": "VariableDeclarator" "type": "VariableDeclarator"
}, },
"end": 21, "end": 23,
"kind": "const", "kind": "const",
"start": 0, "start": 0,
"type": "VariableDeclaration", "type": "VariableDeclaration",
"type": "VariableDeclaration" "type": "VariableDeclaration"
} }
], ],
"end": 21, "end": 23,
"start": 0 "start": 0
} }

View File

@ -1,12 +1,13 @@
--- ---
source: kcl/src/parsing/parser.rs source: kcl/src/parsing/parser.rs
expression: actual expression: actual
snapshot_kind: text
--- ---
{ {
"body": [ "body": [
{ {
"declaration": { "declaration": {
"end": 18, "end": 19,
"id": { "id": {
"end": 3, "end": 3,
"name": "val", "name": "val",
@ -22,9 +23,9 @@ expression: actual
"name": "y" "name": "y"
}, },
"arg": { "arg": {
"end": 17, "end": 18,
"name": "z", "name": "z",
"start": 16, "start": 17,
"type": "Identifier", "type": "Identifier",
"type": "Identifier" "type": "Identifier"
} }
@ -36,7 +37,7 @@ expression: actual
"start": 6, "start": 6,
"type": "Identifier" "type": "Identifier"
}, },
"end": 18, "end": 19,
"start": 6, "start": 6,
"type": "CallExpressionKw", "type": "CallExpressionKw",
"type": "CallExpressionKw", "type": "CallExpressionKw",
@ -51,13 +52,13 @@ expression: actual
"start": 0, "start": 0,
"type": "VariableDeclarator" "type": "VariableDeclarator"
}, },
"end": 18, "end": 19,
"kind": "const", "kind": "const",
"start": 0, "start": 0,
"type": "VariableDeclaration", "type": "VariableDeclaration",
"type": "VariableDeclaration" "type": "VariableDeclaration"
} }
], ],
"end": 18, "end": 19,
"start": 0 "start": 0
} }

View File

@ -23,9 +23,9 @@ pub async fn rem(_exec_state: &mut ExecState, args: Args) -> Result<KclValue, Kc
/// If `num` is negative, the result will be too. /// If `num` is negative, the result will be too.
/// ///
/// ```no_run /// ```no_run
/// assertEqual(rem(7, divisor: 4), 3, 0.01, "remainder is 3") /// assertEqual(rem( 7, divisor = 4), 3, 0.01, "remainder is 3" )
/// assertEqual(rem(-7, divisor: 4), -3, 0.01, "remainder is 3") /// assertEqual(rem(-7, divisor = 4), -3, 0.01, "remainder is -3")
/// assertEqual(rem(7, divisor: -4), 3, 0.01, "remainder is 3") /// assertEqual(rem( 7, divisor = -4), 3, 0.01, "remainder is 3" )
/// ``` /// ```
#[stdlib { #[stdlib {
name = "rem", name = "rem",

View File

@ -272,7 +272,7 @@ impl LabeledArg {
fn recast(&self, options: &FormatOptions, indentation_level: usize, ctxt: ExprContext) -> String { fn recast(&self, options: &FormatOptions, indentation_level: usize, ctxt: ExprContext) -> String {
let label = &self.label.name; let label = &self.label.name;
let arg = self.arg.recast(options, indentation_level, ctxt); let arg = self.arg.recast(options, indentation_level, ctxt);
format!("{label}: {arg}") format!("{label} = {arg}")
} }
} }

View File

@ -196,7 +196,7 @@ snapshot_kind: text
}, },
{ {
"declaration": { "declaration": {
"end": 122, "end": 123,
"id": { "id": {
"end": 103, "end": 103,
"name": "three", "name": "three",
@ -212,9 +212,9 @@ snapshot_kind: text
"name": "delta" "name": "delta"
}, },
"arg": { "arg": {
"end": 121, "end": 122,
"raw": "2", "raw": "2",
"start": 120, "start": 121,
"type": "Literal", "type": "Literal",
"type": "Literal", "type": "Literal",
"value": 2.0 "value": 2.0
@ -227,7 +227,7 @@ snapshot_kind: text
"start": 106, "start": 106,
"type": "Identifier" "type": "Identifier"
}, },
"end": 122, "end": 123,
"start": 106, "start": 106,
"type": "CallExpressionKw", "type": "CallExpressionKw",
"type": "CallExpressionKw", "type": "CallExpressionKw",
@ -243,14 +243,14 @@ snapshot_kind: text
"start": 98, "start": 98,
"type": "VariableDeclarator" "type": "VariableDeclarator"
}, },
"end": 122, "end": 123,
"kind": "const", "kind": "const",
"start": 98, "start": 98,
"type": "VariableDeclaration", "type": "VariableDeclaration",
"type": "VariableDeclaration" "type": "VariableDeclaration"
} }
], ],
"end": 123, "end": 124,
"nonCodeMeta": { "nonCodeMeta": {
"nonCodeNodes": { "nonCodeNodes": {
"0": [ "0": [

View File

@ -7,4 +7,4 @@ fn add(@x, delta) {
} }
two = increment(1) two = increment(1)
three = add(1, delta: 2) three = add(1, delta = 2)

View File

@ -330,8 +330,8 @@ snapshot_kind: text
}, },
{ {
"sourceRange": [ "sourceRange": [
120,
121, 121,
122,
0 0
] ]
} }

View File

@ -85,7 +85,7 @@ snapshot_kind: text
}, },
{ {
"declaration": { "declaration": {
"end": 50, "end": 51,
"id": { "id": {
"end": 38, "end": 38,
"name": "three", "name": "three",
@ -101,9 +101,9 @@ snapshot_kind: text
"name": "x" "name": "x"
}, },
"arg": { "arg": {
"end": 49, "end": 50,
"raw": "1", "raw": "1",
"start": 48, "start": 49,
"type": "Literal", "type": "Literal",
"type": "Literal", "type": "Literal",
"value": 1.0 "value": 1.0
@ -116,7 +116,7 @@ snapshot_kind: text
"start": 41, "start": 41,
"type": "Identifier" "type": "Identifier"
}, },
"end": 50, "end": 51,
"start": 41, "start": 41,
"type": "CallExpressionKw", "type": "CallExpressionKw",
"type": "CallExpressionKw", "type": "CallExpressionKw",
@ -125,14 +125,14 @@ snapshot_kind: text
"start": 33, "start": 33,
"type": "VariableDeclarator" "type": "VariableDeclarator"
}, },
"end": 50, "end": 51,
"kind": "const", "kind": "const",
"start": 33, "start": 33,
"type": "VariableDeclaration", "type": "VariableDeclaration",
"type": "VariableDeclaration" "type": "VariableDeclaration"
} }
], ],
"end": 51, "end": 52,
"nonCodeMeta": { "nonCodeMeta": {
"nonCodeNodes": { "nonCodeNodes": {
"0": [ "0": [

View File

@ -12,6 +12,6 @@ KCL Semantic error
2 │ │ return x + y 2 │ │ return x + y
3 │ ╰─▶ } 3 │ ╰─▶ }
4 │ 4 │
5 │ three = add(x: 1) 5 │ three = add(x = 1)
· ───────── · ─────────
╰──── ╰────

View File

@ -2,4 +2,4 @@ fn add(x, y) {
return x + y return x + y
} }
three = add(x: 1) three = add(x = 1)

View File

@ -78,7 +78,7 @@ snapshot_kind: text
}, },
{ {
"declaration": { "declaration": {
"end": 46, "end": 47,
"id": { "id": {
"end": 34, "end": 34,
"name": "two", "name": "two",
@ -94,9 +94,9 @@ snapshot_kind: text
"name": "x" "name": "x"
}, },
"arg": { "arg": {
"end": 45, "end": 46,
"raw": "1", "raw": "1",
"start": 44, "start": 45,
"type": "Literal", "type": "Literal",
"type": "Literal", "type": "Literal",
"value": 1.0 "value": 1.0
@ -109,7 +109,7 @@ snapshot_kind: text
"start": 37, "start": 37,
"type": "Identifier" "type": "Identifier"
}, },
"end": 46, "end": 47,
"start": 37, "start": 37,
"type": "CallExpressionKw", "type": "CallExpressionKw",
"type": "CallExpressionKw", "type": "CallExpressionKw",
@ -118,14 +118,14 @@ snapshot_kind: text
"start": 31, "start": 31,
"type": "VariableDeclarator" "type": "VariableDeclarator"
}, },
"end": 46, "end": 47,
"kind": "const", "kind": "const",
"start": 31, "start": 31,
"type": "VariableDeclaration", "type": "VariableDeclaration",
"type": "VariableDeclaration" "type": "VariableDeclaration"
} }
], ],
"end": 47, "end": 48,
"nonCodeMeta": { "nonCodeMeta": {
"nonCodeNodes": { "nonCodeNodes": {
"0": [ "0": [

View File

@ -12,6 +12,6 @@ KCL Semantic error
2 │ │ return x + 1 2 │ │ return x + 1
3 │ ╰─▶ } 3 │ ╰─▶ }
4 │ 4 │
5 │ two = add(x: 1) 5 │ two = add(x = 1)
· ───────── · ─────────
╰──── ╰────

View File

@ -2,4 +2,4 @@ fn add(@x) {
return x + 1 return x + 1
} }
two = add(x: 1) two = add(x = 1)

View File

@ -132,7 +132,7 @@ snapshot_kind: text
}, },
{ {
"declaration": { "declaration": {
"end": 98, "end": 99,
"id": { "id": {
"end": 75, "end": 75,
"name": "twentyOne", "name": "twentyOne",
@ -148,9 +148,9 @@ snapshot_kind: text
"name": "by" "name": "by"
}, },
"arg": { "arg": {
"end": 97, "end": 98,
"raw": "20", "raw": "20",
"start": 95, "start": 96,
"type": "Literal", "type": "Literal",
"type": "Literal", "type": "Literal",
"value": 20.0 "value": 20.0
@ -163,7 +163,7 @@ snapshot_kind: text
"start": 78, "start": 78,
"type": "Identifier" "type": "Identifier"
}, },
"end": 98, "end": 99,
"start": 78, "start": 78,
"type": "CallExpressionKw", "type": "CallExpressionKw",
"type": "CallExpressionKw", "type": "CallExpressionKw",
@ -179,14 +179,14 @@ snapshot_kind: text
"start": 66, "start": 66,
"type": "VariableDeclarator" "type": "VariableDeclarator"
}, },
"end": 98, "end": 99,
"kind": "const", "kind": "const",
"start": 66, "start": 66,
"type": "VariableDeclaration", "type": "VariableDeclaration",
"type": "VariableDeclaration" "type": "VariableDeclaration"
} }
], ],
"end": 99, "end": 100,
"nonCodeMeta": { "nonCodeMeta": {
"nonCodeNodes": { "nonCodeNodes": {
"0": [ "0": [

View File

@ -3,4 +3,4 @@ fn increment(@x, by? = 1) {
} }
two = increment(1) two = increment(1)
twentyOne = increment(1, by: 20) twentyOne = increment(1, by = 20)

View File

@ -148,8 +148,8 @@ snapshot_kind: text
}, },
{ {
"sourceRange": [ "sourceRange": [
95, 96,
97, 98,
0 0
] ]
} }

View File

@ -77,8 +77,8 @@ fn setLength = (state, _q) => {
fn Gt2 = (state) => { return setLength(state, state.currentLength * state.factor) } fn Gt2 = (state) => { return setLength(state, state.currentLength * state.factor) }
fn Lt2 = (state) => { return setLength(state, state.currentLength / state.factor) } fn Lt2 = (state) => { return setLength(state, state.currentLength / state.factor) }
fn Add = (state) => { return setAngle(state, rem(int(state.currentAngle - state.angle), divisor: 360)) } fn Add = (state) => { return setAngle(state, rem(int(state.currentAngle - state.angle), divisor = 360)) }
fn Sub = (state) => { return setAngle(state, rem(int(state.currentAngle + state.angle), divisor: 360)) } fn Sub = (state) => { return setAngle(state, rem(int(state.currentAngle + state.angle), divisor = 360)) }
// Only necessary to get around recursion limitations... // Only necessary to get around recursion limitations...
fn F = (state, F) => { fn F = (state, F) => {