Skip to content

Commit

Permalink
Improved documentation. Fixed lexer related bugs. Added executables a…
Browse files Browse the repository at this point in the history
…nd README.
  • Loading branch information
RamziA961 committed Jan 12, 2023
1 parent ddbefc8 commit 37eef03
Show file tree
Hide file tree
Showing 704 changed files with 6,540 additions and 635 deletions.
21 changes: 10 additions & 11 deletions GraphicalUserInterface/ViewModels/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.Diagnostics;
using System.IO;
using System.Reactive;
using DynamicData.Binding;
using ReactiveUI;
using Transpiler;

Expand All @@ -28,10 +27,10 @@ public MainWindowViewModel()

SaveAlgolSource = ReactiveCommand.Create(() =>
{
var path = $"{Directory.GetParent(Environment.CurrentDirectory).Parent.Parent.FullName}/prog.a68";
File.WriteAllText(path, _algolSource);
Output = $"Algol source code successfully saved at {path}";
var path = AppDomain.CurrentDomain.BaseDirectory;
using var f = new StreamWriter(Path.Combine(path, "prog.a68"));
f.Write(_algolSource);
Output = $"Algol source code successfully saved at {path}.";
});
}

Expand Down Expand Up @@ -84,7 +83,7 @@ private void TranspileAlgol()

private void CompileC()
{
var path = Directory.GetParent(Environment.CurrentDirectory).Parent.Parent.FullName;
var path = AppDomain.CurrentDomain.BaseDirectory;;

SaveC();

Expand Down Expand Up @@ -120,10 +119,10 @@ private void CompileC()

private void RunC()
{
var path = Directory.GetParent(Environment.CurrentDirectory).Parent.Parent.FullName;
var path = AppDomain.CurrentDomain.BaseDirectory;
var startInfo = new ProcessStartInfo
{
FileName = $"{path}/prog.o",
FileName = Path.Combine(path, "prog.o"),
Arguments = "",
CreateNoWindow = true,
UseShellExecute = false,
Expand Down Expand Up @@ -153,7 +152,7 @@ private void RunC()

private void LoadAlgol()
{
var path = Directory.GetParent(Environment.CurrentDirectory).Parent.Parent.FullName;
var path = AppDomain.CurrentDomain.BaseDirectory;
var startInfo = new ProcessStartInfo
{
FileName = "cat",
Expand Down Expand Up @@ -185,7 +184,7 @@ private void LoadAlgol()

private void LoadC()
{
var path = Directory.GetParent(Environment.CurrentDirectory).Parent.Parent.FullName;
var path = AppDomain.CurrentDomain.BaseDirectory;;
var startInfo = new ProcessStartInfo
{
FileName = "cat",
Expand Down Expand Up @@ -217,7 +216,7 @@ private void LoadC()

private void SaveC()
{
var path = $"{Directory.GetParent(Environment.CurrentDirectory).Parent.Parent.FullName}/prog.c";
var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "prog.c");
File.WriteAllText(path, _cSource);

Output = $"C source code successfully saved at {path}";
Expand Down
5 changes: 4 additions & 1 deletion GraphicalUserInterface/prog.a68
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
proc hello = (int y, int z) int:
begin
int i = 0;
int i = y + z;
end

proc main = () int:
Expand All @@ -10,5 +10,8 @@ begin
if i == j then
j := j + i;
i := i + i + j + 300;
else
i := j;
hello(i, j);
fi
end
16 changes: 2 additions & 14 deletions GraphicalUserInterface/prog.c
Original file line number Diff line number Diff line change
@@ -1,16 +1,4 @@
int hello (int y,int z) {
const int i = 0;
}

int main () {
int i = 99;
int j = 99;
if (i == j) {
j = j + i;
i = 2 * i + j + 300;
}
else {
i = j + i;
j = 2 * j + i / 200;
};
const float x = 321.032;
float y = -0.54213;
}
3 changes: 2 additions & 1 deletion RALang/AbstractSyntaxTree/AST.fs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ exception SyntaxError of string
exception InvalidOperation of string
exception UnexpectedToken of string


/// Abstract syntax tree type. Instances of this type are created during parsing and all nodes in the
/// program abstract syntax tree are of this type.
type AST = {
children : AST list
decoration : string
Expand Down
82 changes: 38 additions & 44 deletions RALang/AbstractSyntaxTree/AssignTree.fs
Original file line number Diff line number Diff line change
Expand Up @@ -11,54 +11,48 @@ open Transpiler.AbstractSyntaxTree.IdentifierTree
<assignment> := <decl>'='(<expr>|<string>) | <identifier>':='(<expr>|<string>)
*)

let isAssign (tokens : Token list) : bool =
/// <summary>
/// Active pattern that matches Assignments.
/// </summary>
/// <param name="tokens">A token list.</param>
/// <returns>Some if the first sequence of tokens is an assignment, otherwise None. </returns>
let (|Assignment|_|) (tokens: Token list) =
match tokens with
| TYPE _ :: _ ->
isDecl tokens[0..1]
&& match tokens[2] with
| ASSIGN | MUTATE ->
true
| _ -> false
&& (isExpr tokens[3..] || match tokens[3..] with S _ :: _ -> true | _ -> false)
| IDENTIFIER _ :: _ ->
match tokens[1] with
| MUTATE ->
true
| _ -> false
&& (isExpr tokens[2..] || match tokens[3..] with S _ :: _ -> true | _ -> false)
| _ -> false
| IDENTIFIER _ :: MUTATE :: _ -> Some(tokens)
| TYPE _ :: IDENTIFIER _ :: (ASSIGN | MUTATE) :: _ -> Some(tokens)
| _ -> None

/// <summary>
/// Attempts to create an Assignment/Mutate AST. It can consist of a Declaration AST and
/// an Expression/String AST or an Identifier AST and an Expression/String AST.
/// </summary>
/// <param name="tokens">A token list.</param>
/// <returns>Unparsed token list and an Assign/Mutate AST or None.</returns>
let Assign (tokens: Token list) : Token list * AST option =

let Assignment (tokens : Token list, ast : AST option) =
match tokens, ast with
| _, None -> tokens, ast
| ASSIGN | MUTATE as tok :: tail, Some ast ->
// match tail with String or Expression
let remTokens, expr = match tail with
| S s :: tail -> tail, Some { token = S s; children = []; decoration = "CharTree" }
| _ -> Expr tail


let Assign (tokens: Token list) : Token list * AST =
let Assignment (tokens : Token list, ast : AST) =
match tokens with
| ASSIGN :: tail ->
let remTokens, expr =
match tail with
| S s :: tail -> tail, { token = S s; children = []; decoration = "charTree" }
| _ -> Expr tail
(remTokens, {
children = [ast; expr]
decoration = "AssignTree"
token = tokens.Head
})
| MUTATE :: tail ->
let remTokens, expr =
match tail with
| S s :: tail -> tail, { token = S s; children = []; decoration = "charTree" }
| _ -> Expr tail
(remTokens, {
children = [ast; expr]
decoration = "MutateTree"
token = tokens.Head
})
| _ -> raise(UnexpectedToken $"Unexpected token encountered: {tokens[0]}. Expected ASSIGN or MUTATE.")
match expr with
| Some expr -> remTokens, Some {
children = [ast; expr]
decoration = match tok with | ASSIGN -> "AssignTree" | MUTATE -> "MutateTree"
token = tok
}
| None -> tokens, None // propagate None upwards.
| _ -> tokens, ast

let NonTerminal (tokens: Token list) : Token list * AST =
let NonTerminal (tokens: Token list) : Token list * AST option =
match tokens with
| TYPE _ :: _ -> Decl tokens
| IDENTIFIER _ :: _ -> Identifier tokens
| _ -> raise(UnexpectedToken $"Unexpected token encountered: {tokens[0]}. Expected TYPE or IDENTIFIER.")
| TYPE _ :: _ -> Decl tokens // Create a Declaration AST
| IDENTIFIER _ :: _ -> Identifier tokens // Create an identifier AST
| _ -> tokens, None

(NonTerminal >> Assignment) tokens

Loading

0 comments on commit 37eef03

Please sign in to comment.