Skip to content

Commit

Permalink
Create standard library (#232)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tinggaard authored Apr 26, 2024
1 parent 124afc6 commit 4a1e923
Show file tree
Hide file tree
Showing 17 changed files with 563 additions and 23 deletions.
2 changes: 1 addition & 1 deletion SocietalConstructionTool/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
{
Console.WriteLine("Warning: No output specified");
}
_ = SctRunner.CompileAndRun(sourceFiles.Select(f => f.FullName).ToArray(), logger);
_ = SctRunner.CompileAndRun(sourceFiles.Select(f => f.FullName), logger);
}, sourceFilesArgument, outputToConsoleOption, outputFileOption);


Expand Down
68 changes: 68 additions & 0 deletions SocietalConstructionTool/Resources/Stdlib.sct
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
class stdCounter(int x) {
state Counting {
if (x <= 0) {
exit;
}
x = x - 1;
enter Counting;
}
}

class stdPredicateExists(Predicate p) {
state ExitWhen {
if (exists(p)) {
exit;
}
enter ExitWhen;
}

state ExitWhenNo {
if (!exists(p)) {
exit;
}
enter ExitWhenNo;
}
}

class stdPredicateAmount(Predicate p, int x) {
state ExitLessThan {
if (count(p) < x) {
exit;
}
enter ExitLessThan;
}

state ExitMoreThan {
if (count(p) > x) {
exit;
}
enter ExitMoreThan;
}
}



// exit the simulation after the specified amount of ticks
function ExitAfterTicks(int ticks) -> void {
create stdCounter::Counting(x: ticks);
}

// exit the simulation when there exists an agent fulfilling `pred`
function ExitWhenExists(Predicate pred) -> void {
create stdPredicateExists::ExitWhen(p: pred);
}

// exit the simulation when there no longer exists any agents fulfilling `pred`
function ExitWhenNoLongerExists(Predicate pred) -> void {
create stdPredicateExists::ExitWhenNo(p: pred);
}

// exit the simulation when there exists `amount` agents fulfilling `pred`
function ExitWhenMoreThan(Predicate pred, int amount) -> void {
create stdPredicateAmount::ExitMoreThan(p: pred, x: amount);
}

// exit the simulation when there are less than `amount` agents fulfilling `pred`
function ExitWhenLessThan(Predicate pred, int amount) -> void {
create stdPredicateAmount::ExitLessThan(p: pred, x: amount);
}
30 changes: 10 additions & 20 deletions SocietalConstructionTool/SctRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,20 @@ private static SctParser GetSctParser(string input)
return parser;
}

private static IEnumerable<string> StdLibFilename =>
Directory.GetFiles(Path.Join(AppDomain.CurrentDomain.BaseDirectory, "Resources"));

/**
* <summary>
* Reads an SCT source file, statically chekcs it and translates it into C# code
* </summary>
* <param name="filenames">The path of the SCT source file</param>
* <returns>The resulting C# source, or null if compilation failed</returns>
*/
public static (string? outputText, IEnumerable<CompilerError> errors) CompileSct(string[] filenames)
public static (string? outputText, IEnumerable<CompilerError> errors) CompileSct(IEnumerable<string> filenames)
{
// Make SctTableVisitor take a CTableBuilder as a parameter
// Analyse each file separately
// Add file name to each found error.
// Call CTabelBuilder.BuildCtable() after all files have been visited
// Run the translator on all files concatenated.
// Add stdlib to the list of files to compile
filenames = filenames.Concat(StdLibFilename);

var errors = RunStaticChecks(filenames);

Expand Down Expand Up @@ -115,7 +115,7 @@ private static List<CompilerError> RunSecondPassChecks(ParserRuleContext startNo
return typeChecker.Errors.ToList();
}

public static List<CompilerError> RunStaticChecks(string[] filenames)
public static List<CompilerError> RunStaticChecks(IEnumerable<string> filenames)
{
// Create a CTableBuilder that is used for all files.
CTableBuilder cTableBuilder = new();
Expand Down Expand Up @@ -243,9 +243,8 @@ private static void Run(Assembly assembly, IRuntimeContext initialContext)
* <param name="filename">The path of the SCT source file</param>
* <param name="logger">The logger to use to output the result of the simulation</param>
*/
public static IEnumerable<CompilerError> CompileAndRun(string[] filenames, IOutputLogger? logger)
public static IEnumerable<CompilerError> CompileAndRun(IEnumerable<string> filenames, IOutputLogger? logger)
{

var (outputText, errors) = CompileSct(filenames);

// TODO: Handle errors from ANTLR. They are not currently being passed to the errors list.
Expand Down Expand Up @@ -273,16 +272,7 @@ public static IEnumerable<CompilerError> CompileAndRun(string[] filenames, IOutp
return [];
}

private static string ConcatenateFiles(string[] filenames)
{

string result = string.Empty;
foreach (var file in filenames)
{
result += File.ReadAllText(file);
}

return result;
}
private static string ConcatenateFiles(IEnumerable<string> filenames)
=> filenames.Select(File.ReadAllText).Aggregate((acc, next) => acc + next);
}
}
5 changes: 5 additions & 0 deletions SocietalConstructionTool/SocietalConstructionTool.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@
<PackageReference Include="Microsoft.CodeAnalysis" Version="4.7.0 " />
<PackageReference Include="System.CommandLine" Version="2.0.0-beta4.22272.1" />
</ItemGroup>
<ItemGroup>
<Content Include="Resources\**">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="../SctBuildTasks/SctBuildTasks.csproj" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
[
{
"State": "Counting",
"Fields": {
"x": 3
},
"ClassName": "stdCounter"
}
][
{
"State": "Counting",
"Fields": {
"x": 2
},
"ClassName": "stdCounter"
}
][
{
"State": "Counting",
"Fields": {
"x": 1
},
"ClassName": "stdCounter"
}
][
{
"State": "Counting",
"Fields": {
"x": 0
},
"ClassName": "stdCounter"
}
][]
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
[
{
"State": "Bar",
"Fields": {},
"ClassName": "Foo"
},
{
"State": "ExitWhen",
"Fields": {
"p": {
"ClassName": "__sct_Foo",
"State": "__sct_Baz",
"Fields": {}
}
},
"ClassName": "stdPredicateExists"
}
][
{
"State": "Baz",
"Fields": {},
"ClassName": "Foo"
},
{
"State": "ExitWhen",
"Fields": {
"p": {
"ClassName": "__sct_Foo",
"State": "__sct_Baz",
"Fields": {}
}
},
"ClassName": "stdPredicateExists"
}
][
{
"State": "Baz",
"Fields": {},
"ClassName": "Foo"
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
[
{
"State": "Bar",
"Fields": {},
"ClassName": "Foo"
},
{
"State": "Baz",
"Fields": {},
"ClassName": "Foo"
},
{
"State": "Baz",
"Fields": {},
"ClassName": "Foo"
},
{
"State": "ExitLessThan",
"Fields": {
"p": {
"ClassName": "__sct_Foo",
"State": null,
"Fields": {}
},
"x": 2
},
"ClassName": "stdPredicateAmount"
}
][
{
"State": "Baz",
"Fields": {},
"ClassName": "Foo"
},
{
"State": "ExitLessThan",
"Fields": {
"p": {
"ClassName": "__sct_Foo",
"State": null,
"Fields": {}
},
"x": 2
},
"ClassName": "stdPredicateAmount"
}
][]
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
[
{
"State": "Bar",
"Fields": {},
"ClassName": "Foo"
},
{
"State": "ExitMoreThan",
"Fields": {
"p": {
"ClassName": "__sct_Foo",
"State": "__sct_Baz",
"Fields": {}
},
"x": 1
},
"ClassName": "stdPredicateAmount"
}
][
{
"State": "Baz",
"Fields": {},
"ClassName": "Foo"
},
{
"State": "ExitMoreThan",
"Fields": {
"p": {
"ClassName": "__sct_Foo",
"State": "__sct_Baz",
"Fields": {}
},
"x": 1
},
"ClassName": "stdPredicateAmount"
}
][
{
"State": "Bar",
"Fields": {},
"ClassName": "Foo"
},
{
"State": "Baz",
"Fields": {},
"ClassName": "Foo"
},
{
"State": "ExitMoreThan",
"Fields": {
"p": {
"ClassName": "__sct_Foo",
"State": "__sct_Baz",
"Fields": {}
},
"x": 1
},
"ClassName": "stdPredicateAmount"
}
][
{
"State": "Baz",
"Fields": {},
"ClassName": "Foo"
},
{
"State": "Bar",
"Fields": {},
"ClassName": "Foo"
},
{
"State": "Baz",
"Fields": {},
"ClassName": "Foo"
},
{
"State": "ExitMoreThan",
"Fields": {
"p": {
"ClassName": "__sct_Foo",
"State": "__sct_Baz",
"Fields": {}
},
"x": 1
},
"ClassName": "stdPredicateAmount"
}
][
{
"State": "Bar",
"Fields": {},
"ClassName": "Foo"
},
{
"State": "Baz",
"Fields": {},
"ClassName": "Foo"
},
{
"State": "Baz",
"Fields": {},
"ClassName": "Foo"
},
{
"State": "Bar",
"Fields": {},
"ClassName": "Foo"
},
{
"State": "Baz",
"Fields": {},
"ClassName": "Foo"
}
]
Loading

0 comments on commit 4a1e923

Please sign in to comment.