Skip to content

Commit 6f258fa

Browse files
more better
1 parent 3c13fbb commit 6f258fa

37 files changed

Lines changed: 431 additions & 374 deletions

Code/ArgumentSystem/Arguments/BoolArgument.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace SER.Code.ArgumentSystem.Arguments;
1010

1111
public class BoolArgument(string name) : Argument(name)
1212
{
13-
public override string InputDescription => "boolean (true or false) value";
13+
public override string InputDescription => "bool (true or false) value";
1414

1515
public bool IsFunction { get; init; } = false;
1616

Code/ContextSystem/Contexter.cs

Lines changed: 65 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using SER.Code.ContextSystem.BaseContexts;
2+
using SER.Code.ContextSystem.Contexts;
23
using SER.Code.ContextSystem.Contexts.Control;
34
using SER.Code.ContextSystem.Interfaces;
45
using SER.Code.Extensions;
@@ -138,23 +139,82 @@ List<RunnableContext> contexts
138139

139140
if (firstToken is not IContextableToken contextable)
140141
{
141-
return $"'{firstToken.RawRep}' is not a valid way to start a line. Perhaps you made a typo?";
142+
return rs + $"'{firstToken.RawRep}' is not a valid way to start a line. Perhaps you made a typo?";
142143
}
143144

144145
var context = contextable.GetContext(scr);
145146
if (context is null) return context;
146-
147-
foreach (var token in tokens.Skip(1))
147+
148+
bool endLineContexting = false;
149+
for (var index = 1; index < tokens.Length; index++)
148150
{
149-
if (HandleCurrentContext(token, context, out var endLineContexting).HasErrored(out var errorMsg))
150-
return rs + errorMsg;
151+
var token = tokens[index];
152+
rs = $"Cannot add token {token} to {context}";
153+
if (AttemptsInlineWithKeyword(token, context))
154+
{
155+
if (HandleInlineWithKeyword(tokens.Skip(index), context, scr).HasErrored(out var error))
156+
{
157+
return rs + error;
158+
}
159+
160+
break;
161+
}
162+
163+
if (token is CommentToken)
164+
{
165+
return context;
166+
}
151167

168+
if (HandleCurrentContext(token, context, out endLineContexting).HasErrored(out var errorMsg))
169+
return rs + errorMsg;
170+
152171
if (endLineContexting) break;
153172
}
154173

155174
return context;
156175
}
157176

177+
private static bool AttemptsInlineWithKeyword(BaseToken token, Context currentContext)
178+
{
179+
return token is IContextableToken contextable
180+
&& contextable.GetContext(token.Script) is WithKeyword
181+
&& currentContext is StatementContext;
182+
}
183+
184+
private static Result HandleInlineWithKeyword(IEnumerable<BaseToken> enumTokens, RunnableContext context, Script scr)
185+
{
186+
var tokens = enumTokens.ToArray();
187+
188+
if (tokens.First() is not IContextableToken contextable2
189+
|| contextable2.GetContext(scr) is not WithKeyword
190+
|| context is not StatementContext statement)
191+
{
192+
return $"{context.FriendlyName} does not accept {tokens.First()}";
193+
}
194+
195+
if (ContextLine(tokens, null, scr).HasErrored(out var contextError, out var contextResult))
196+
{
197+
return contextError;
198+
}
199+
200+
if (contextResult is not WithKeyword with)
201+
{
202+
return $"{contextResult.FriendlyName} does not accept {tokens.First()}";
203+
}
204+
205+
if (with.AcceptStatement(statement).HasErrored(out var acceptError))
206+
{
207+
return acceptError;
208+
}
209+
210+
if (with.VerifyCurrentState().HasErrored(out var verifyError))
211+
{
212+
return verifyError;
213+
}
214+
215+
return true;
216+
}
217+
158218
private static Result HandleCurrentContext(BaseToken token, RunnableContext context, out bool endLineContexting)
159219
{
160220
Result rs = $"Cannot add '{token.RawRep}' to {context}";

Code/ContextSystem/Contexts/Control/Loops/BreakKeyword.cs renamed to Code/ContextSystem/Contexts/Control/BreakKeyword.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ public class BreakKeyword : StandardContext, IKeywordContext
1313
public string KeywordName => "break";
1414

1515
public string Description =>
16-
"Makes a given loop (that the 'break' keyword is inside) act as it has completely ended its execution " +
17-
"(\"breaks\" free from the loop)";
16+
"Makes a given loop or function (that the 'break' keyword is inside) act as it has completely ended its execution " +
17+
"(\"breaks\" free from the loop/function)";
1818

1919
public string[] Arguments => [];
2020

@@ -31,6 +31,16 @@ wait 1s
3131
break
3232
end
3333
end
34+
35+
func Test
36+
if {Chance 20%}
37+
break
38+
end
39+
40+
Print "this will not run because the 'break' keyword was used"
41+
end
42+
43+
run Test
3444
""";
3545

3646
public override string FriendlyName => "'break' keyword";

Code/ContextSystem/Contexts/Control/Loops/ForeverLoop.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
using SER.Code.ContextSystem.Interfaces;
44
using SER.Code.ContextSystem.Structures;
55
using SER.Code.Helpers.ResultSystem;
6-
using SER.Code.MethodSystem.BaseMethods;
76
using SER.Code.Plugin;
87
using SER.Code.TokenSystem.Tokens;
98
using SER.Code.ValueSystem;
@@ -33,9 +32,7 @@ wait 2m
3332
# ========================================
3433
# you can also use "with" keyword to define an iteration variable
3534
# which will hold the current iteration number, starting from 1
36-
forever
37-
with $iter
38-
35+
forever with $iter
3936
wait 1s
4037
Print "current iteration: {$iter}"
4138
end

Code/ContextSystem/Contexts/Control/Loops/OverLoop.cs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,13 @@ over @all
3838
# additionally, "over" loop can tell you which item is currently being iterated over
3939
# this is usually known as a "for each" loop in other languages
4040
# this can be done using "with" keyword and naming a temporary variable:
41-
over @all
42-
with @plr
43-
41+
over @all with @plr
4442
Print "found player {@plr -> name}"
4543
end
4644
4745
# this also works for collections:
4846
&inventory = @sender -> inventory
49-
over &inventory
50-
with *item
51-
47+
over &inventory with *item
5248
Print "found item {*item -> type}"
5349
end
5450
# its important to remember that the variable type in "with" keyword
@@ -58,9 +54,7 @@ with @plr
5854
# ========================================
5955
# "with" can also define a second variable, which will hold the index of the current item
6056
# this is a number value starting at 1, and incrementing by 1 for each iteration
61-
over @all
62-
with @plr $index
63-
57+
over @all with @plr $index
6458
Print "found player #{$index}: {@plr -> name}"
6559
end
6660
""";

Code/ContextSystem/Contexts/Control/Loops/RepeatLoop.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,7 @@ repeat 10
3333
3434
# ========================================
3535
# you can also define a variable which will hold the current iteration number, starting from 1
36-
repeat 10
37-
with $iter
36+
repeat 10 with $iter
3837
3938
Print "current iteration: {$iter}"
4039
end

Code/ContextSystem/Contexts/Control/Loops/WhileLoop.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,7 @@ wait 1s
3030
3131
# ========================================
3232
# you may also use a "with" keyword to define an iteration variable
33-
while {Chance 90%}
34-
with $iter
35-
33+
while {Chance 90%} with $iter
3634
Print "current attempt to leave loop: {$iter}"
3735
wait 1s
3836
end

Code/ContextSystem/Contexts/Control/OnErrorStatement.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,8 @@ public class OnErrorStatement : StatementContext, IStatementExtender, IKeywordCo
2929
Print "Hello, world!"
3030
# ^ won't get executed because 'attempt' skips the remaining code
3131
# inside of it if an error was made
32-
on_error
33-
with $message $type $stackTrace
3432
33+
on_error with $message $type $stackTrace
3534
# this will print the error message
3635
Print "Error: {$message}"
3736

Code/ContextSystem/Contexts/Control/ReturnKeyword.cs

Lines changed: 10 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -14,67 +14,42 @@ namespace SER.Code.ContextSystem.Contexts.Control;
1414
[UsedImplicitly]
1515
public class ReturnKeyword : StandardContext, IKeywordContext
1616
{
17-
private IValueToken? _returnValueToken;
18-
private (RunnableContext main, IMayReturnValueContext returner)? _returnContext = null;
17+
private ValueExpressionContext? _expression = null;
1918

2019
public string KeywordName => "return";
2120
public string Description => "Returns value when in a function.";
2221
public string[] Arguments => ["[return value]"];
2322
public string? Example => null;
2423

2524
public override string FriendlyName => "'return' keyword";
26-
27-
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2825

2926
public override TryAddTokenRes TryAddToken(BaseToken token)
3027
{
31-
if (_returnContext.HasValue)
32-
{
33-
return _returnContext.Value.main.TryAddToken(token);
34-
}
28+
if (_expression is not null) return _expression.TryAddToken(token);
3529

36-
switch (token)
30+
_expression = new ValueExpressionContext(token, true)
3731
{
38-
case IContextableToken contextable when
39-
contextable.GetContext(Script) is { } mainContext and IMayReturnValueContext returnValueContext:
40-
{
41-
_returnContext = (mainContext, returnValueContext);
42-
return TryAddTokenRes.Continue();
43-
}
44-
case IValueToken valToken:
45-
{
46-
_returnValueToken = valToken;
47-
return TryAddTokenRes.End();
48-
}
49-
default:
50-
return TryAddTokenRes.Error($"Expected to receive a value or method, but received '{token.RawRep}' instead.");
51-
}
32+
Script = token.Script
33+
};
34+
35+
return TryAddTokenRes.Continue();
5236
}
5337

5438
public override Result VerifyCurrentState()
5539
{
5640
return Result.Assert(
57-
_returnValueToken != null || _returnContext.HasValue,
41+
_expression is not null,
5842
"Return value was not provided."
5943
);
6044
}
6145

6246
protected override void Execute()
6347
{
64-
Value value;
65-
if (_returnContext.HasValue)
66-
{
67-
value = _returnContext.Value.returner.ReturnedValue
68-
?? throw new ScriptRuntimeError(this,
69-
$"{_returnContext.Value.main} has not returned a value. " +
70-
$"{_returnContext.Value.returner.MissingValueHint}"
71-
);
72-
}
73-
else if (_returnValueToken!.Value().HasErrored(out var error, out value!))
48+
if (_expression!.GetValue().HasErrored(out var error, out var value))
7449
{
7550
throw new ScriptRuntimeError(this, error);
7651
}
77-
52+
7853
ParentContext?.SendControlMessage(new Return(value));
7954
}
8055
}

Code/ContextSystem/Contexts/FuncStatement.cs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,31 @@ public class FuncStatement :
2828
public string KeywordName => "func";
2929
public string Description => "Defines a function.";
3030
public string[] Arguments => ["[function name]"];
31-
public string? Example => null;
31+
32+
public string? Example =>
33+
"""
34+
func $Add with $a $b
35+
return $a + $b
36+
end
37+
38+
$sum = run $Add 5 3
39+
Print $sum
40+
41+
42+
func @SigmasOnly
43+
return RemovePlayers * @classDPlayers
44+
end
45+
46+
@sigmas = run @SigmasOnly
47+
Explode @sigmas
48+
49+
50+
func ExplodeAll
51+
Explode *
52+
end
53+
54+
run ExplodeAll
55+
""";
3256

3357
// gets the type of value associated with a token type of a variable prefix
3458
// sketchy!!

0 commit comments

Comments
 (0)