diff options
| author | jwansek <eddie.atten.ea29@gmail.com> | 2021-11-01 14:51:47 +0000 | 
|---|---|---|
| committer | jwansek <eddie.atten.ea29@gmail.com> | 2021-11-01 14:51:47 +0000 | 
| commit | 85e2726ddedd2981425c5ac07f7257bce1a6ddbf (patch) | |
| tree | d84a75438cba4fd3d7180c776f3632577292433a | |
| parent | 69b0ad07bac30beca1397ff187468e7597203c44 (diff) | |
| download | esotericFORTRAN-85e2726ddedd2981425c5ac07f7257bce1a6ddbf.tar.gz esotericFORTRAN-85e2726ddedd2981425c5ac07f7257bce1a6ddbf.zip  | |
started work on translation to c
| -rw-r--r-- | .gitignore | 4 | ||||
| -rw-r--r-- | code/FORTRAN2C/Makefile | 8 | ||||
| -rw-r--r-- | code/FORTRAN2C/build/out.c | 6 | ||||
| -rw-r--r-- | code/FORTRAN2C/examples/maths.txt | 1 | ||||
| -rw-r--r-- | code/FORTRAN2C/examples/maths2.txt | 1 | ||||
| -rw-r--r-- | code/FORTRAN2C/examples/maths3.txt | 1 | ||||
| -rw-r--r-- | code/FORTRAN2C/examples/maths4.txt | 1 | ||||
| -rw-r--r-- | code/FORTRAN2C/fortran2c.grammar (renamed from code/simpleSableCCCalulator/sableCCCalculator.grammar) | 2 | ||||
| -rw-r--r-- | code/FORTRAN2C/fortran2c/CWriter.java | 26 | ||||
| -rw-r--r-- | code/FORTRAN2C/fortran2c/Compiler.java | 28 | ||||
| -rw-r--r-- | code/FORTRAN2C/fortran2c/Translation.java | 13 | ||||
| -rw-r--r-- | code/simpleSableCCCalulator/sableCCCalculator/Translation.java | 1 | 
12 files changed, 90 insertions, 2 deletions
@@ -3,6 +3,10 @@ code/simpleSableCCCalulator/sableCCCalculator/analysis/  code/simpleSableCCCalulator/sableCCCalculator/lexer/  code/simpleSableCCCalulator/sableCCCalculator/node/  code/simpleSableCCCalulator/sableCCCalculator/parser/ +code/FORTRAN2C/fortran2c/analysis/ +code/FORTRAN2C/fortran2c/lexer/ +code/FORTRAN2C/fortran2c/node/ +code/FORTRAN2C/fortran2c/parser/  # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider  # Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 diff --git a/code/FORTRAN2C/Makefile b/code/FORTRAN2C/Makefile new file mode 100644 index 0000000..87610cf --- /dev/null +++ b/code/FORTRAN2C/Makefile @@ -0,0 +1,8 @@ +all:  +	java -jar ../../../sablecc-3.7/lib/sablecc.jar fortran2c.grammar +	 +clean: +	rm -rfv fortran2c/analysis/ +	rm -rfv fortran2c/lexer/ +	rm -rfv fortran2c/node/ +	rm -rvf fortran2c/parser/ diff --git a/code/FORTRAN2C/build/out.c b/code/FORTRAN2C/build/out.c new file mode 100644 index 0000000..47d2d58 --- /dev/null +++ b/code/FORTRAN2C/build/out.c @@ -0,0 +1,6 @@ +#include <stdio.h> +#include <stdlib.h> + +int main(int argc, char** argv) { + +}
\ No newline at end of file diff --git a/code/FORTRAN2C/examples/maths.txt b/code/FORTRAN2C/examples/maths.txt new file mode 100644 index 0000000..36726f5 --- /dev/null +++ b/code/FORTRAN2C/examples/maths.txt @@ -0,0 +1 @@ +(36/2 + 45.2) * 3
\ No newline at end of file diff --git a/code/FORTRAN2C/examples/maths2.txt b/code/FORTRAN2C/examples/maths2.txt new file mode 100644 index 0000000..313296b --- /dev/null +++ b/code/FORTRAN2C/examples/maths2.txt @@ -0,0 +1 @@ +sin(45 * 2) / 3 diff --git a/code/FORTRAN2C/examples/maths3.txt b/code/FORTRAN2C/examples/maths3.txt new file mode 100644 index 0000000..e092af1 --- /dev/null +++ b/code/FORTRAN2C/examples/maths3.txt @@ -0,0 +1 @@ +3-1+2
\ No newline at end of file diff --git a/code/FORTRAN2C/examples/maths4.txt b/code/FORTRAN2C/examples/maths4.txt new file mode 100644 index 0000000..a922b77 --- /dev/null +++ b/code/FORTRAN2C/examples/maths4.txt @@ -0,0 +1 @@ +2 + 2 diff --git a/code/simpleSableCCCalulator/sableCCCalculator.grammar b/code/FORTRAN2C/fortran2c.grammar index 426fac1..80bf2b7 100644 --- a/code/simpleSableCCCalulator/sableCCCalculator.grammar +++ b/code/FORTRAN2C/fortran2c.grammar @@ -1,4 +1,4 @@ -Package sableCCCalculator; +Package fortran2c;  Helpers      digit = ['0' .. '9'];  Tokens diff --git a/code/FORTRAN2C/fortran2c/CWriter.java b/code/FORTRAN2C/fortran2c/CWriter.java new file mode 100644 index 0000000..fec748a --- /dev/null +++ b/code/FORTRAN2C/fortran2c/CWriter.java @@ -0,0 +1,26 @@ +package fortran2c; + +import java.io.*; +import java.util.HashSet; + +public class CWriter { +     +    private String filePath; +    private File theFile; +    private FileWriter theFileWriter; +    private HashSet<String> functions; + +    public CWriter(String filePath) throws IOException { +        this.filePath = filePath; +        File theFile = new File(filePath); + +        if (theFile.createNewFile()) { +            FileWriter theFileWriter = new FileWriter(filePath); +            theFileWriter.write("#include <stdio.h>\n#include <stdlib.h>\n\nint main(int argc, char** argv) {\n}"); +            theFileWriter.close(); +            functions.add("main"); +        } else { +            throw new IOException("The file already exists"); +        } +    } +} diff --git a/code/FORTRAN2C/fortran2c/Compiler.java b/code/FORTRAN2C/fortran2c/Compiler.java new file mode 100644 index 0000000..7b124fa --- /dev/null +++ b/code/FORTRAN2C/fortran2c/Compiler.java @@ -0,0 +1,28 @@ +package fortran2c; +import fortran2c.parser.*; +import fortran2c.lexer.*; +import fortran2c.node.*; +import java.io.*; + +public class Compiler +{ +    public static void main(String[] args) +    { +        try +        { +            System.out.println("Using source file: " + args[0]); +            // Create a Parser instance. +            Parser p = new Parser(new Lexer(new PushbackReader(new InputStreamReader(new FileInputStream(args[0])), 1024))); +            // Parse the input. +            Start tree = p.parse(); +            // Apply the translation. +            CWriter cWriter = new CWriter("build/out.c"); + +            tree.apply(new Translation(cWriter)); +        } +        catch(Exception e) +        { +            System.out.println(e.getMessage()); +        } +    } +}
\ No newline at end of file diff --git a/code/FORTRAN2C/fortran2c/Translation.java b/code/FORTRAN2C/fortran2c/Translation.java new file mode 100644 index 0000000..f5eeaba --- /dev/null +++ b/code/FORTRAN2C/fortran2c/Translation.java @@ -0,0 +1,13 @@ +package fortran2c; +import fortran2c.analysis.*; +import fortran2c.node.*; + +class Translation extends DepthFirstAdapter +{ + +    private CWriter writer; + +    public Translation(CWriter writer) { +        this.writer = writer; +    } +} diff --git a/code/simpleSableCCCalulator/sableCCCalculator/Translation.java b/code/simpleSableCCCalulator/sableCCCalculator/Translation.java index 737ecbd..43041c4 100644 --- a/code/simpleSableCCCalulator/sableCCCalculator/Translation.java +++ b/code/simpleSableCCCalulator/sableCCCalculator/Translation.java @@ -1,5 +1,4 @@  package sableCCCalculator; -import sableCCCalculator.SymbolTable.SymbolTableIndex;  import sableCCCalculator.analysis.*;  import sableCCCalculator.types.*;  import sableCCCalculator.node.*;  | 
