forked from UbiquityDotNET/Llvm.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReplEngine.cs
More file actions
61 lines (52 loc) · 1.85 KB
/
Copy pathReplEngine.cs
File metadata and controls
61 lines (52 loc) · 1.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// Copyright (c) Ubiquity.NET Contributors. All rights reserved.
// Licensed under the Apache-2.0 WITH LLVM-exception license. See the LICENSE.md file in the project root for full license information.
using System;
using Kaleidoscope.Grammar;
using Kaleidoscope.Runtime;
using Ubiquity.NET.Llvm.Values;
using Ubiquity.NET.Runtime.Utils;
namespace Kaleidoscope.Chapter6
{
internal class ReplEngine
: ReadEvaluatePrintLoopBase<Value>
{
public ReplEngine( )
: base( LanguageLevel.UserDefinedOperators )
{
}
public override ICodeGenerator<Value> CreateGenerator( DynamicRuntimeState state )
{
return new CodeGenerator( state );
}
public override void ProcessResults( Value resultValue )
{
switch(resultValue)
{
case ConstantFP result:
// If the cursor is not at the beginning of a line
// generate a new line for it
if(Console.CursorLeft > 0)
{
Console.WriteLine();
}
Console.WriteLine( "{0}", result.Value );
break;
case Function function:
#if SAVE_LLVM_IR
if(function.Name is not null)
{
string safeFileName = Utilities.GetSafeFileName( function.Name );
string finalPath = System.IO.Path.ChangeExtension( safeFileName, "ll" );
if(!function.ParentModule.WriteToTextFile( finalPath, out string errMessage ))
{
Console.Error.WriteLine($"ERROR saving output file '{finalPath}': {errMessage}");
}
}
#endif
break;
default:
throw new InvalidOperationException();
}
}
}
}