Skip to content

Commit abe3694

Browse files
Add a reaf from file function and a version var
1 parent 161049e commit abe3694

File tree

4 files changed

+27
-16
lines changed

4 files changed

+27
-16
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ crit.exe
55
CritLang/Properties/PublishProfiles/FolderProfile.pubxml
66
CritLang/Properties/PublishProfiles/FolderProfile.pubxml.user
77
CritLang/CritLang.csproj.user
8+
CritLang/Content/ola.txt

CritLang/Content/test.crit

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,11 @@
11

2-
str_num1 = ReadLine("Digite um numero: ");
3-
str_num2 = ReadLine("Digite outro numero: ");
42

5-
int_num1 = Convert(str_num1, "int");
6-
int_num2 = Convert(str_num2, "int");
3+
path = "./Content/ola.txt";
74

8-
WriteLine(int_num1 + int_num2);
95

6+
text = ReadText(path);
107

11-
lista = [1, 2, "tres", 69];
128

13-
num = 0;
9+
WriteLine(text + CritVersion);
1410

15-
while num < Len(lista) {
16-
WriteLine(lista[num]);
17-
num += 1;
18-
}
19-
20-
21-
WriteLine("FINAL");
2211

CritLang/CritVisitor.cs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ public class CritVisitor: CritBaseVisitor<object?>
1010

1111

1212

13-
public CritVisitor()
13+
public CritVisitor(string version)
1414
{
15+
string _version = version;
1516
//Math Functions
1617
Variables["PI"] = Math.PI;
1718
Variables["Sqrt"] = new Func<object?[], object?>(Sqrt);
@@ -28,9 +29,29 @@ public CritVisitor()
2829
Variables["Remove"] = new Func<object?[], object?>(RemoveArr);
2930
Variables["Len"] = new Func<object?[], object?>(LenArr);
3031

32+
//OS Functions
33+
Variables["ReadText"] = new Func<object?[], object?>(ReadText);
3134

3235
//Gerenal Functions
3336
Variables["Convert"] = new Func<object?[], object?>(ConvertTo);
37+
Variables["CritVersion"] = _version;
38+
39+
}
40+
41+
42+
public static object? ReadText(object?[] args)
43+
{
44+
if (args.Length != 1) throw new Exception("ReadText expects 1 argument");
45+
46+
try
47+
{
48+
return File.ReadAllText(args[0]!.ToString()!);
49+
}
50+
catch (Exception e)
51+
{
52+
throw new Exception("ReadText failed: " + e.Message);
53+
}
54+
3455

3556
}
3657

CritLang/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,6 @@
3131
var commonTokenStream = new CommonTokenStream(critLexer);
3232
var critParser = new CritParser(commonTokenStream);
3333
var critContext = critParser.program();
34-
var visitor = new CritVisitor();
34+
var visitor = new CritVisitor(VERSION);
3535

3636
visitor.Visit(critContext);

0 commit comments

Comments
 (0)