Skip to content

Commit 8979a1f

Browse files
Improve context documentation and examples
1 parent fb43504 commit 8979a1f

34 files changed

Lines changed: 1130 additions & 314 deletions

Code/Builders/Builder.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1676,9 +1676,18 @@ private static void CreateSerMethodInfo()
16761676
.OrderBy(keyword => keyword.KeywordName)
16771677
.ToDictionary(keyword => keyword.KeywordName, object (keyword) => new
16781678
{
1679-
syntax = $"{keyword.KeywordName} {string.Join(" ", keyword.Arguments)}".TrimEnd(),
1679+
syntax = keyword.Usage,
1680+
usage = keyword.Usage,
16801681
description = keyword.Description,
1681-
arguments = keyword.Arguments,
1682+
arguments = keyword.Arguments.Select(argument => argument.Syntax).ToArray(),
1683+
argumentDetails = keyword.Arguments.Select(argument => new
1684+
{
1685+
syntax = argument.Syntax,
1686+
description = argument.Description,
1687+
inputDescription = argument.InputDescription,
1688+
optional = argument.IsOptional,
1689+
consumesRemainingValues = argument.ConsumesRemainingValues
1690+
}).ToArray(),
16821691
example = keyword.Example,
16831692
isStatement = keyword is StatementContext,
16841693
extendsSignal = (keyword as IStatementExtender)?.Extends.ToString(),

Code/ContextSystem/BaseContexts/Context.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,15 @@ public abstract class Context
99
{
1010
public required Script Script { get; set; } = null!;
1111

12+
public virtual string Usage => this is SER.Code.ContextSystem.Interfaces.IKeywordContext keyword
13+
? $"{keyword.KeywordName} {string.Join(" ", keyword.Arguments.Select(argument => argument.Syntax))}".TrimEnd()
14+
: FriendlyName;
15+
1216
public abstract string FriendlyName { get; }
1317

1418
public abstract TryAddTokenRes TryAddToken(BaseToken token);
1519

1620
public abstract Result VerifyCurrentState();
1721

1822
public abstract override string ToString();
19-
}
23+
}

Code/ContextSystem/BaseContexts/LoopContext.cs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,21 @@ public abstract class LoopContext : StatementContext, IKeywordContext
1111

1212
protected bool ReceivedContinue;
1313

14-
protected abstract string? Usage { get; }
14+
protected abstract string? DetailedUsage { get; }
1515

1616
public sealed override string FriendlyName => $"'{KeywordName}' loop statement";
1717
public Dictionary<IExtendableStatement.Signal, StatementContext> RegisteredSignals { get; } = [];
1818

1919
public abstract string KeywordName { get; }
2020
public abstract string Description { get; }
21-
public abstract string[] Arguments { get; }
21+
public abstract ContextArgument[] Arguments { get; }
2222

2323
public string Example => ExampleHandler.GetExample($"{KeywordName}KeywordExample") ??
24-
$"""
25-
{Usage}
24+
$"""
25+
{DetailedUsage}
2626
27-
# ========================================
28-
# "break" and "continue" keywords work as usual and you are free to use them inside "{KeywordName}" loops
29-
""";
27+
# Use break to leave the loop or continue to skip to its next iteration.
28+
""";
3029

3130
protected override void OnReceivedControlMessageFromChild(ParentContextControlMessage msg)
3231
{

Code/ContextSystem/Contexts/Control/AttemptStatement.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,17 @@ public class AttemptStatement : StatementContext, IExtendableStatement, IKeyword
2020
"Runs everything inside the statement, and if something throws an exception (error), the error will not " +
2121
"terminate the script. If there is an 'on_error' statement, it will be executed.";
2222

23-
public string[] Arguments => [];
23+
public ContextArgument[] Arguments => [];
2424
public string Example =>
2525
"""
2626
&collection = Coll.Empty
27-
# swallows the error (doesn't stop the script)
2827
attempt
2928
Print {Coll.Fetch &collection 2}
30-
# throws because there's nothing at index 2
29+
end
30+
31+
# The error is handled by on_error when one is present.
32+
on_error with $message
33+
Print "Handled: {$message}"
3134
end
3235
""";
3336

Code/ContextSystem/Contexts/Control/BreakKeyword.cs

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,30 +15,18 @@ public class BreakKeyword : StandardContext, IKeywordContext
1515
"Makes a given loop or function (that the 'break' keyword is inside) act as it has completely ended its execution " +
1616
"(\"breaks\" free from the loop/function)";
1717

18-
public string[] Arguments => [];
18+
public ContextArgument[] Arguments => [];
1919

2020
public string Example =>
21-
"""
22-
# for example:
23-
forever
24-
wait 1s
25-
26-
Print "attempting to leave forever loop"
27-
if {Chance 20%}
28-
break
29-
end
30-
end
31-
32-
func Test
33-
if {Chance 20%}
34-
break
35-
end
36-
37-
Print "this will not run because the 'break' keyword was used"
38-
end
39-
40-
run Test
41-
""";
21+
"""
22+
# the execution will "break" free after the third iteration
23+
repeat 10 with $iteration
24+
if {$iteration is 3}
25+
break
26+
end
27+
Print "iteration {$iteration}"
28+
end
29+
""";
4230

4331
public override TryAddTokenRes TryAddToken(BaseToken token)
4432
{
@@ -54,4 +42,4 @@ protected override void Execute()
5442
{
5543
ParentContext?.SendControlMessage(new Break());
5644
}
57-
}
45+
}

Code/ContextSystem/Contexts/Control/ChanceStatement.cs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,21 @@ public class ChanceStatement : StatementContext, IExtendableStatement, IKeywordC
2222
public Dictionary<IExtendableStatement.Signal, StatementContext> RegisteredSignals { get; } = [];
2323
public string KeywordName => "chance";
2424
public string Description => "This statement will execute with the provided chance.";
25-
public string[] Arguments => ["$chance"];
26-
public string? Example => null;
25+
public ContextArgument[] Arguments => [ContextArgument.Required(
26+
"$chance", "Probability that the body will execute.",
27+
"A number from 0 to 1, where 1 is 100%.")];
28+
public string Example =>
29+
"""
30+
chance 25%
31+
Print "This happens about one time in four."
32+
end
33+
34+
# The chance can also come from a value expression.
35+
$probability = 50%
36+
chance $probability
37+
Print "This happens about half the time."
38+
end
39+
""";
2740

2841
public override TryAddTokenRes TryAddToken(BaseToken token)
2942
{

Code/ContextSystem/Contexts/Control/ElifStatement.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,19 @@ public class ElifStatement : StatementContext, IStatementExtender, IExtendableSt
2323
public string KeywordName => "elif";
2424
public string Description =>
2525
"If the statement above it didn't execute, 'elif' statement will try to execute if the provided condition is met.";
26-
public string[] Arguments => ["$condition"];
27-
public string? Example => null;
26+
public ContextArgument[] Arguments => [ContextArgument.Required(
27+
"$condition", "Expression checked when the preceding branch did not execute.",
28+
"A boolean expression; values and method calls may be grouped with braces.")];
29+
public string Example =>
30+
"""
31+
if {@sender -> role} is "ClassD"
32+
Print "You are a Class-D."
33+
elif {@sender -> role} is "Scientist"
34+
Print "You are a scientist."
35+
else
36+
Print "You have another role."
37+
end
38+
""";
2839

2940
public IExtendableStatement.Signal Extends => IExtendableStatement.Signal.DidntExecute;
3041

Code/ContextSystem/Contexts/Control/ElseStatement.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,15 @@ public class ElseStatement : StatementContext, IStatementExtender, IKeywordConte
1414
public string KeywordName => "else";
1515
public string Description =>
1616
"If the statement above it didn't execute, 'else' statement will execute instead.";
17-
public string[] Arguments => [];
18-
public string? Example => null;
19-
20-
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
17+
public ContextArgument[] Arguments => [];
18+
public string Example =>
19+
"""
20+
if {@sender -> team} is "SCPs"
21+
Reply "You are an SCP"
22+
else
23+
Reply "You are not an SCP"
24+
end
25+
""";
2126

2227
public IExtendableStatement.Signal Extends => IExtendableStatement.Signal.DidntExecute;
2328

Code/ContextSystem/Contexts/Control/EndKeyword.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,13 @@ public class EndKeyword : StandardContext, IKeywordContext
1212
public override string FriendlyName => "'end' keyword";
1313
public string KeywordName => "end";
1414
public string Description => "Ends the current statement's body.";
15-
public string[] Arguments => [];
16-
public string? Example => null;
15+
public ContextArgument[] Arguments => [];
16+
public string Example =>
17+
"""
18+
if {@sender -> role} is "ClassD"
19+
Print "This block ends here."
20+
end
21+
""";
1722

1823
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
1924

@@ -30,4 +35,4 @@ public override Result VerifyCurrentState()
3035
protected override void Execute()
3136
{
3237
}
33-
}
38+
}

Code/ContextSystem/Contexts/Control/IfStatement.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,19 @@ public class IfStatement : StatementContext, IExtendableStatement, IKeywordConte
2222
public Dictionary<IExtendableStatement.Signal, StatementContext> RegisteredSignals { get; } = [];
2323
public string KeywordName => "if";
2424
public string Description => "This statement will execute only if the provided condition is met.";
25-
public string[] Arguments => ["$condition"];
26-
public string? Example => null;
25+
public ContextArgument[] Arguments => [ContextArgument.Required(
26+
"$condition", "Expression that must evaluate to true for the body to run.",
27+
"A boolean expression; values and method calls may be grouped with braces.")];
28+
public string Example =>
29+
"""
30+
if {@sender -> isAlive}
31+
Print "You are still alive."
32+
end
33+
34+
if {AmountOf @all} > 0
35+
Print "There is at least one player online."
36+
end
37+
""";
2738

2839
public override TryAddTokenRes TryAddToken(BaseToken token)
2940
{

0 commit comments

Comments
 (0)