Skip to content

Commit 1e3a71c

Browse files
Added a Read from stdin function and a convert one
1 parent 34bf2d5 commit 1e3a71c

File tree

5 files changed

+93
-23
lines changed

5 files changed

+93
-23
lines changed

CritLang/CommandLineArgs.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22
{
33
internal class CommandLineArgs
44
{
5+
/// <summary>
6+
/// Parse the arguments passed to the program.
7+
/// </summary>
8+
/// <param name="args"></param>
9+
/// <param name="version"></param>
10+
/// <returns></returns>
511
public static string Parse(string[] args, string version)
612
{
713
foreach (var arg in args)

CritLang/Content/Crit.g4

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ program: line* EOF;
55

66
line: statement | ifBlock | whileBlock;
77

8+
89
statement: (assignment|functionCall) ';';
910

1011
ifBlock: 'if' expression block ('else' elseIfBlock);
@@ -31,6 +32,7 @@ expression
3132
| expression addOp expression #additiveExpression
3233
| expression compareOp expression #comparisonExpression
3334
| expression boolOp expression #booleanExpression
35+
// | expression INCREMENTOP #incrementExpression TODO add incrementOP
3436
;
3537

3638

@@ -52,11 +54,13 @@ array: '[' (constant (',' constant)*)? ']';
5254
//index: '[' INTEGER ']';
5355
NULL: 'null';
5456

57+
INDEX: '[' IDENTIFIER ']' | '[' INTEGER ']';
58+
INCREMENTOP: '++' | '--';
5559

5660
block: '{' line* '}';
5761

5862
WS: [ \t\r\n]+ -> skip;
59-
IDENTIFIER: [a-zA-Z_][a-zA-Z0-9_]* ('[' IDENTIFIER ']' | '[' INTEGER ']')* ;
63+
IDENTIFIER: [a-zA-Z_][a-zA-Z0-9_]* (INDEX)* ;
6064

6165
Comment
6266
: '#' ~( '\r' | '\n' )*

CritLang/Content/test.crit

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,22 @@
1-
ola = 5;
2-
3-
4-
5-
ola *= 2;
6-
7-
ola /= 3;
8-
9-
ola -= 1.3333;
10-
11-
ola += 3;
12-
13-
ola %= 2
14-
15-
WriteLine(ola);
16-
1+

2+
str_num1 = ReadLine("Digite um numero: ");
3+
str_num2 = ReadLine("Digite outro numero: ");
174

5+
int_num1 = Convert(str_num1, "int");
6+
int_num2 = Convert(str_num2, "int");
187

8+
WriteLine(int_num1 + int_num2);
199

2010

11+
lista = [1, 2, "tres", 69];
2112

13+
num = 0;
2214

15+
while num < Len(lista) {
16+
WriteLine(lista[num]);
17+
num += 1;
18+
}
2319

2420

21+
WriteLine("FINAL");
2522

CritLang/CritVisitor.cs

Lines changed: 66 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Globalization;
2+
using System.Reflection.Metadata;
23
using CritLang.Content;
34

45
namespace CritLang;
@@ -11,20 +12,65 @@ public class CritVisitor: CritBaseVisitor<object?>
1112

1213
public CritVisitor()
1314
{
14-
//GLOBAL FUNCTIONs / VARIABLES
15+
//Math Functions
1516
Variables["PI"] = Math.PI;
1617
Variables["Sqrt"] = new Func<object?[], object?>(Sqrt);
1718
Variables["Pow"] = new Func<object?[], object?>(Pow);
1819

20+
//Console Functions
1921
Variables["Write"] = new Func<object?[], object?>(Write);
2022
Variables["WriteLine"] = new Func<object?[], object?>(WriteLine);
23+
Variables["ReadLine"] = new Func<object?[], object?>(ReadLine);
24+
25+
//Array Functions
2126
Variables["Sum"] = new Func<object?[], object?>(SumArr);
2227
Variables["Add"] = new Func<object?[], object?>(AddArr);
2328
Variables["Remove"] = new Func<object?[], object?>(RemoveArr);
2429
Variables["Len"] = new Func<object?[], object?>(LenArr);
30+
31+
32+
//Gerenal Functions
33+
Variables["Convert"] = new Func<object?[], object?>(ConvertTo);
34+
2535
}
2636

2737

38+
public static object? ConvertTo(object?[] args)
39+
{
40+
if (args.Length != 2)
41+
{
42+
throw new Exception("ConvertTo expects 2 arguments, first one the variable to convert to and the second one being the type.");
43+
}
44+
45+
string[] typeOptions = new[] { "int", "float", "string", "bool" };
46+
if (!typeOptions.Contains(args[1]!.ToString()!))
47+
throw new Exception($"Invalid type...\nMust be one of the following types: {string.Join(", ", typeOptions)}");
48+
49+
return args[1]!.ToString() switch
50+
{
51+
"int" => Convert.ToInt32(args[0]),
52+
"float" => Convert.ToSingle(args[0]),
53+
"string" => Convert.ToString(args[0]),
54+
"bool" => Convert.ToBoolean(args[0]),
55+
_ => throw new Exception("Invalid type...")
56+
};
57+
}
58+
59+
60+
private static object? ReadLine(object?[] args)
61+
{
62+
if (args.Length != 1)
63+
{
64+
throw new Exception("ReadLine expects 1 arguments, being the text to prompt to the use.");
65+
}
66+
67+
string text = args[0]!.ToString()!;
68+
Console.Write(text);
69+
return Console.ReadLine();
70+
71+
}
72+
73+
2874
private static object? RemoveArr(object?[] args)
2975
{
3076
if (args.Length != 2)
@@ -251,10 +297,8 @@ public CritVisitor()
251297
public override object? VisitIdentifierExpression(CritParser.IdentifierExpressionContext context)
252298
{
253299
var varName = context.IDENTIFIER().GetText();
254-
255300
if (varName.Contains('[') && varName.Contains(']'))
256301
{
257-
258302
string[] variableHelper = varName.Replace("]", string.Empty).Split('[');
259303
string varWithoutIndex = variableHelper[0];
260304
string index = variableHelper[1];
@@ -277,6 +321,25 @@ public CritVisitor()
277321
throw new Exception($"Variable {varWithoutIndex} not found");
278322
}
279323
}
324+
325+
326+
//TODO ADD SOMETHING LIKE THIS
327+
//if (varName.Contains('.'))
328+
//{az
329+
// string[] variableHelper = varName.Split('.');
330+
// string varWithoutIndex = variableHelper[0];
331+
// string index = variableHelper[1];
332+
// var variable = Variables[varWithoutIndex];
333+
// if (variable is not null)
334+
// {
335+
// var value = variable.GetType().GetProperty(index).GetValue(variable);
336+
// return value;
337+
// }
338+
// else
339+
// {
340+
// throw new Exception($"Variable {varWithoutIndex} not found");
341+
// }
342+
//}
280343

281344
if (!Variables.ContainsKey(varName))
282345
throw new Exception($"Variable '{varName}' is not defined");

CritLang/Program.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
using CritLang;
33
using CritLang.Content;
44

5-
string VERSION = "v0.1.7-beta";
5+
string VERSION = "v0.1.8-beta";
66

77
string fileName = CommandLineArgs.Parse(args, VERSION);
88

@@ -30,7 +30,7 @@
3030
var critLexer = new CritLexer(inputStream);
3131
var commonTokenStream = new CommonTokenStream(critLexer);
3232
var critParser = new CritParser(commonTokenStream);
33-
var chatContext = critParser.program();
33+
var critContext = critParser.program();
3434
var visitor = new CritVisitor();
3535

36-
visitor.Visit(chatContext);
36+
visitor.Visit(critContext);

0 commit comments

Comments
 (0)