├── .gitattributes
├── .github
└── workflows
│ └── build-and-release.yml
├── .gitignore
├── COPYRIGHT-examples
├── LICENSE
├── README.md
├── THIRD-PARTY
├── build.gradle
├── examples
├── Closure.cafe
├── List.cafe
├── Object.cafe
└── merge_sort.cafe
├── gradle
├── innosetup
│ ├── cafe_exe_compiler.iss
│ ├── innoinstall.sh
│ ├── iscc
│ └── logo.ico
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
├── lib
└── asm-7.3.1.jar
├── settings.gradle
└── src
├── main
└── java
│ ├── compiler
│ ├── Main.java
│ ├── analyzer
│ │ ├── PrettyPrinter.java
│ │ ├── SemanticsChecker.java
│ │ ├── Symbol.java
│ │ └── SymbolTable.java
│ ├── ast
│ │ ├── Node.java
│ │ ├── Tree.java
│ │ ├── TreeVisitor.java
│ │ └── package-info.java
│ ├── gen
│ │ ├── ASTToCafeIrVisitor.java
│ │ ├── AbstractCafeIrVisitor.java
│ │ ├── AnnFuncNameGenerator.java
│ │ ├── ClosureReferenceVisitor.java
│ │ ├── JVMByteCodeGenVisitor.java
│ │ ├── JVMBytecodeUtils.java
│ │ └── SymbolReferenceAssignmentVisitor.java
│ ├── ir
│ │ ├── AnonymousFunction.java
│ │ ├── AssignedStatement.java
│ │ ├── AssignmentStatement.java
│ │ ├── BinaryExpression.java
│ │ ├── Block.java
│ │ ├── BreakContinueStatement.java
│ │ ├── CafeClosure.java
│ │ ├── CafeElement.java
│ │ ├── CafeExport.java
│ │ ├── CafeFunction.java
│ │ ├── CafeImport.java
│ │ ├── CafeIrVisitor.java
│ │ ├── CafeModule.java
│ │ ├── CafeStatement.java
│ │ ├── ConditionalBranching.java
│ │ ├── ConstantStatement.java
│ │ ├── DeclarativeAssignmentStatement.java
│ │ ├── ExpressionStatement.java
│ │ ├── ForLoopStatement.java
│ │ ├── FunctionInvocation.java
│ │ ├── FunctionWrapper.java
│ │ ├── ListCollection.java
│ │ ├── MethodInvocation.java
│ │ ├── NullStatement.java
│ │ ├── ObjectAccessStatement.java
│ │ ├── ObjectCreationStatement.java
│ │ ├── OperatorType.java
│ │ ├── PropertyAccess.java
│ │ ├── ReferenceLookup.java
│ │ ├── ReferenceTable.java
│ │ ├── ReturnStatement.java
│ │ ├── SliceExpression.java
│ │ ├── SubscriptExpression.java
│ │ ├── SymbolReference.java
│ │ ├── TargetFuncWrapper.java
│ │ ├── ThisStatement.java
│ │ └── UnaryExpression.java
│ ├── main
│ │ ├── CafeCompiler.java
│ │ ├── CompilerResult.java
│ │ ├── Main.java
│ │ ├── SourceFileManager.java
│ │ ├── cli
│ │ │ ├── Command.java
│ │ │ ├── CompileCommand.java
│ │ │ └── RunCommand.java
│ │ └── package-info.java
│ ├── parser
│ │ ├── CharReader.java
│ │ ├── Lexer.java
│ │ ├── MainParser.java
│ │ ├── Parser.java
│ │ ├── ParserFactory.java
│ │ ├── ParserType.java
│ │ ├── Scanner.java
│ │ ├── ScannerFactory.java
│ │ ├── Tokenizer.java
│ │ ├── Tokens.java
│ │ └── package-info.java
│ └── util
│ │ ├── Context.java
│ │ ├── Log.java
│ │ ├── Messages.java
│ │ ├── Position.java
│ │ └── package-info.java
│ ├── library
│ ├── DFunc.java
│ ├── DList.java
│ ├── DObject.java
│ ├── Slicable.java
│ ├── Subscriptable.java
│ ├── base
│ │ ├── CFunc.java
│ │ ├── CFuncProto.java
│ │ ├── CList.java
│ │ ├── CListProto.java
│ │ ├── CObject.java
│ │ └── CObjectProto.java
│ └── io
│ │ └── BasicIO.java
│ └── runtime
│ ├── CafeClassLoader.java
│ ├── CafeURLClassLoader.java
│ ├── DObjectCreator.java
│ ├── ExportMap.java
│ ├── ImportEvaluator.java
│ ├── JavaImports.java
│ ├── ReferenceSymbol.java
│ ├── ReferenceTable.java
│ ├── Runtime.java
│ ├── imports
│ ├── CafeModulePath.java
│ ├── ImportPathVisitor.java
│ ├── JavaModulePath.java
│ └── ModulePath.java
│ └── indy
│ ├── FunctionInvocationID.java
│ ├── FunctionReferenceID.java
│ ├── ImportID.java
│ ├── MethodInvocationID.java
│ ├── ObjectAccessID.java
│ ├── OperatorID.java
│ ├── SliceID.java
│ └── SubscriptID.java
└── test
├── java
├── compiler
│ ├── CompileAndCheckTest.java
│ ├── CompileAndExecuteTest.java
│ ├── CompileTest.java
│ ├── gen
│ │ └── AnnFuncNameGeneratorTest.java
│ └── parser
│ │ └── MainParserTest.java
└── testing
│ └── Utils.java
└── resources
├── ann-func-test
├── anonymous_func_export.cafe
├── anonymous_func_import.cafe
└── as-closure
│ ├── ann_func_export.cafe
│ └── ann_func_import.cafe
├── compile-and-execute
├── ann_func.cafe
├── bind_args.cafe
├── closure.cafe
├── default_import_object_test.cafe
├── for_loop.cafe
├── function_call.cafe
├── list.cafe
└── missing_pop_if_else.cafe
├── compile
└── list.cafe
└── parse-test
├── error_test
├── for_loop_error.cafe
└── slice_subscript_error.cafe
├── if_else.cafe
└── slice_subscript.cafe
/.gitattributes:
--------------------------------------------------------------------------------
1 | #
2 | # https://help.github.com/articles/dealing-with-line-endings/
3 | #
4 | # These are explicitly windows files and should use crlf
5 | *.bat text eol=crlf
6 |
7 |
--------------------------------------------------------------------------------
/.github/workflows/build-and-release.yml:
--------------------------------------------------------------------------------
1 | # This workflow will build a Java project with Gradle
2 | # For more information see: https://help.github.com/actions/language-and-framework-guides/building-and-testing-java-with-gradle
3 |
4 | name: Build and Release
5 |
6 | on:
7 | push:
8 | branches:
9 | - master
10 | tags:
11 | - v*
12 | pull_request:
13 | branches:
14 | - master
15 |
16 | jobs:
17 | build:
18 |
19 | runs-on: ubuntu-latest
20 |
21 | steps:
22 | - uses: actions/checkout@v2
23 | - name: Set up JDK 1.8
24 | uses: actions/setup-java@v1
25 | with:
26 | java-version: 1.8
27 | - name: Cache Gradle packages
28 | uses: actions/cache@v2
29 | with:
30 | path: ~/.gradle/caches
31 | key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle') }}
32 | restore-keys: ${{ runner.os }}-gradle
33 | - name: Grant execute permission for gradlew
34 | run: chmod +x gradlew
35 |
36 | - name: Inno Setup
37 | run: |
38 | set -x
39 | sudo apt-get install -y -q innoextract
40 | sudo dpkg --add-architecture i386 && sudo apt-get update && sudo apt-get install wine32
41 | wine --version
42 | innoextract --version
43 | chmod +x ./gradle/innosetup/innoinstall.sh
44 | ./gradle/innosetup/innoinstall.sh
45 | chmod +x gradle/innosetup/iscc
46 | sudo cp -p gradle/innosetup/iscc /usr/local/bin/iscc
47 | iscc /? 2> /dev/null | grep "Inno Setup Preprocessor"
48 | echo "Here3"
49 |
50 | - name: Build with Gradle
51 | run: |
52 | source ~/.bash_profile
53 | ./gradlew build exe --stacktrace
54 |
55 | - name: Copy build distribution
56 | run: |
57 | cp build/distributions/*.zip cafe-distribution.zip
58 | cp build/innosetup/*.exe cafe.exe
59 |
60 | - name: Attach build distribution from this build
61 | uses: actions/upload-artifact@v2
62 | with:
63 | name: Cafe distribution from this build
64 | path: |
65 | ./cafe-distribution.zip
66 | ./cafe.exe
67 |
68 | - name: Create a release
69 | id: create_release
70 | if: startsWith(github.ref, 'refs/tags/')
71 | uses: actions/create-release@v1
72 | env:
73 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
74 | with:
75 | tag_name: ${{ github.ref }}
76 | release_name: Release ${{ github.ref }}
77 | draft: false
78 | prerelease: false
79 |
80 | - name: Attach build distribution to the release
81 | if: startsWith(github.ref, 'refs/tags/')
82 | uses: actions/upload-release-asset@v1
83 | env:
84 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
85 | with:
86 | upload_url: ${{ steps.create_release.outputs.upload_url }}
87 | asset_path: ./cafe-distribution.zip
88 | asset_name: cafe-distribution.zip
89 | asset_content_type: application/zip
90 |
91 | - name: Attach windows binary to the release
92 | if: startsWith(github.ref, 'refs/tags/')
93 | uses: actions/upload-release-asset@v1
94 | env:
95 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
96 | with:
97 | upload_url: ${{ steps.create_release.outputs.upload_url }}
98 | asset_path: ./cafe.exe
99 | asset_name: cafe.exe
100 | asset_content_type: application/octet-stream
101 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | /bin/
2 |
3 | # Ignore Gradle project-specific cache directory
4 | .gradle
5 |
6 | # Ignore Gradle build output directory
7 | build
8 |
9 | # Eclipse Core
10 | .project
11 |
12 | # JDT-specific (Eclipse Java Development Tools)
13 | .classpath
14 |
15 | cp.cafe
16 | /.idea/
17 | /.vscode/
18 | /.settings/
19 |
--------------------------------------------------------------------------------
/COPYRIGHT-examples:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright (c) 2021. The Cafe Authors
3 |
4 | This file is part of Cafe.
5 |
6 | Cafe is free software: you can redistribute it and/or modify
7 | it under the terms of the GNU General Public License as published by
8 | the Free Software Foundation, version 3 of the License.
9 |
10 | Cafe is distributed in the hope that it will be useful,
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | GNU General Public License for more details.
14 |
15 | You should have received a copy of the GNU General Public License
16 | along with Cafe. If not, see .
17 | */
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # CAFE: Programming Language
2 |
3 | [](https://github.com/cafe-jvm-lang/cafe/actions?query=workflow%3ABuild)
4 | [](https://www.gnu.org/licenses/gpl-3.0)
5 | [](#)
6 | [](https://gitter.im/cafe-jvm-lang/cafe-lang?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
7 |
8 | A simple, dynamic, weakly typed, prototype based language for the JVM.
9 | Built with `invokedynamic` instruction introduced in Java 7, Cafe takes advantage of latest advances of JVM.
10 |
11 | This project is developed with an intent to explore `how a programming language runs internally` and is also a major project for the graduation year.
12 |
13 | ---
14 | **NOTE**
15 |
16 | This project is still under development.
17 |
18 | ---
19 |
20 | ## Authors
21 | In order to get in touch with Cafe Authors, join
22 | [cafe-jvm-lang/cafe-lang](https://gitter.im/cafe-jvm-lang/cafe-lang) (Gitter).
23 | | | username | talk to me about... |
24 | --------------------------------------------------------------------------------------------------|----------------------------------------------------------------|---------------------------------------------------|
25 |
| [`@dhyey-shah`](https://github.com/dhyey-shah) | cafe grammar,semantic-analyzer,compiler back-end (IR,code-generation), runtime |
26 |
| [`@SaurabhPethani`](https://github.com/SaurabhPethani) | compiler front-end (RD parser, ast) |
27 |
| [`@supastrikaromil`](https://github.com/supastrikaromil) | cafe grammar, compiler front-end (Tokenizer) |
28 |
29 | ## Building CAFE
30 | Cafe is built with [Gradle](https://gradle.org).
31 | Since the source code contains the [Gradle wrapper scripts](https://docs.gradle.org/current/userguide/gradle_wrapper.html),
32 | the build can bootstrap itself by downloading the qualified Gradle version from the Internet.
33 |
34 | ### Java virtual machine compatibility
35 |
36 | Cafe requires Java 8 to build.
37 |
38 | In practice you can run most Cafe code with Java 8 and beyond.
39 |
40 | ### Building
41 |
42 | This project is built with [IntelliJ IDEA](https://www.jetbrains.com/idea/) and can be loaded as a gradle project. I believe any other IDEs which support gradle can load this project.
43 |
44 | #### Gradle Java plugin
45 |
46 | For running as a java project, [java-application-plugin](https://docs.gradle.org/current/userguide/java_plugin.html) is already included and can be invoked with `gradlew run`.
47 | The source file to compile can be set from `build.gradle` `run` method.
48 |
49 | ## License
50 | GNU General Public License v3.0.
51 |
52 | See LICENSE to see the full text.
53 |
54 | ## Contributing
55 |
56 | We are cuurently closed for contributions.
57 |
--------------------------------------------------------------------------------
/THIRD-PARTY:
--------------------------------------------------------------------------------
1 | Binary and source distribution of Cafe include 3rd party software not written by Cafe developers.
2 |
3 | # ASM Library (OW2 Consortium)
4 |
5 | See http://asm.ow2.org/
6 |
7 | Published under the terms of a BSD-style license:
8 |
9 | Copyright (c) 2000-2011 INRIA, France Telecom
10 | All rights reserved.
11 |
12 | Redistribution and use in source and binary forms, with or without
13 | modification, are permitted provided that the following conditions
14 | are met:
15 |
16 | 1. Redistributions of source code must retain the above copyright
17 | notice, this list of conditions and the following disclaimer.
18 |
19 | 2. Redistributions in binary form must reproduce the above copyright
20 | notice, this list of conditions and the following disclaimer in the
21 | documentation and/or other materials provided with the distribution.
22 |
23 | 3. Neither the name of the copyright holders nor the names of its
24 | contributors may be used to endorse or promote products derived from
25 | this software without specific prior written permission.
26 |
27 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
28 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
31 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
37 | THE POSSIBILITY OF SUCH DAMAGE.
38 |
39 | # JCommander
40 |
41 | See http://jcommander.org/
42 |
43 | Copyright 2010 Cedric Beust
44 |
45 | Licensed under the Apache License, Version 2.0 (the "License");
46 | you may not use this file except in compliance with the License.
47 | You may obtain a copy of the License at
48 |
49 | http://www.apache.org/licenses/LICENSE-2.0
50 |
51 | Unless required by applicable law or agreed to in writing, software
52 | distributed under the License is distributed on an "AS IS" BASIS,
53 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
54 | See the License for the specific language governing permissions and
55 | limitations under the License.
56 |
57 | # Jansi
58 |
59 | See https://fusesource.github.io/jansi/
60 |
61 | Copyright (C) 2009-2017 the original author(s).
62 |
63 | Licensed under the Apache License, Version 2.0 (the "License");
64 | you may not use this file except in compliance with the License.
65 | You may obtain a copy of the License at
66 |
67 | http://www.apache.org/licenses/LICENSE-2.0
68 |
69 | Unless required by applicable law or agreed to in writing, software
70 | distributed under the License is distributed on an "AS IS" BASIS,
71 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
72 | See the License for the specific language governing permissions and
73 | limitations under the License.
74 |
--------------------------------------------------------------------------------
/examples/Closure.cafe:
--------------------------------------------------------------------------------
1 | func A(){
2 | func B(){
3 | cmd.println("In B");
4 | }
5 | return B;
6 | }
7 |
8 | var a = A();
9 | a();
--------------------------------------------------------------------------------
/examples/List.cafe:
--------------------------------------------------------------------------------
1 | var x = {a:10};
2 | var z = "string";
3 | var l = [1,[2,3],z];
4 | l[1][0] = "new";
5 | cmd.println(l);
6 |
7 | x["l"] = l;
8 | x.l[1][0] = "new 2";
9 | x.l[0:1] = 3;
10 | cmd.println(x);
11 |
12 | cmd.println(l[1:3]);
13 | l[2:3] = ["slice1","slice2"];
14 | cmd.println(l);
15 |
16 | l[0:3] = "a";
17 | cmd.println(l);
18 |
19 | l.add("last element");
20 | l.remove(z);
21 | l.removeAt(0);
22 | cmd.println(l);
23 |
24 |
--------------------------------------------------------------------------------
/examples/Object.cafe:
--------------------------------------------------------------------------------
1 | var acc = {
2 | init: func(){
3 | this.amount = 0;
4 | },
5 | updateBal: func(amt){
6 | this.amount = this.amount+amt;
7 | },
8 | checkBalance: func(){
9 | cmd.println(this.amount);
10 | },
11 | deposit: func(b){
12 | this.updateBal(b);
13 | },
14 | withdraw: func(b){
15 | if(this.amount < b){
16 | cmd.println("Insufficient Balance");
17 | } else {
18 | updateBal(-b);
19 | }
20 | }
21 | };
22 |
23 | acc.init();
24 | acc.checkBalance();
25 | acc.deposit(1000);
26 | acc.withdraw(2000);
27 | acc.checkBalance();
--------------------------------------------------------------------------------
/examples/merge_sort.cafe:
--------------------------------------------------------------------------------
1 | const merge = func(a,b){
2 | const c = [];
3 |
4 | for(;a.size()> 0 and b.size() > 0;){
5 | var obj;
6 |
7 | if(a.get(0) > b.get(0)){
8 | obj = b.get(0);
9 | b.removeAt(0);
10 | }else{
11 | obj = a.get(0);
12 | a.removeAt(0);
13 | }
14 | c.add(obj);
15 | }
16 |
17 | for(;a.size()>0;){
18 | c.add(a.get(0));
19 | a.removeAt(0);
20 | }
21 |
22 | for(;b.size()>0;){
23 | c.add(b.get(0));
24 | b.removeAt(0);
25 | }
26 |
27 | return c;
28 | },
29 | merge_sort = func(a){
30 | if (a.size() < 2){
31 | return a;
32 | }
33 |
34 | const middle = floor(a.size()/2);
35 | const a_l = a[0:middle];
36 | const a_r = a[middle:a.size()];
37 | const sorted_l = merge_sort(a_l);
38 | const sorted_r = merge_sort(a_r);
39 | return merge(sorted_l, sorted_r);
40 | };
41 |
42 | func floor(a){
43 | const dec = a % 1;
44 | if(dec < 0.5){ a = a-dec; }
45 | else{ a = a + (1-dec); }
46 |
47 | return a;
48 | }
49 |
50 | cmd.println(merge_sort([5,4,3,2,1]));
--------------------------------------------------------------------------------
/gradle/innosetup/cafe_exe_compiler.iss:
--------------------------------------------------------------------------------
1 | ; Script generated by the Inno Setup Script Wizard.
2 | ; SEE THE DOCUMENTATION FOR DETAILS ON CREATING INNO SETUP SCRIPT FILES!
3 |
4 | #define MyAppName "Cafe Compiler"
5 | #define MyAppPublisher "Cafe"
6 | #define MyAppURL "https://www.cafe-lang.tech/"
7 |
8 | [Setup]
9 | ; NOTE: The value of AppId uniquely identifies this application. Do not use the same AppId value in installers for other applications.
10 | ; (To generate a new GUID, click Tools | Generate GUID inside the IDE.)
11 | AppId=Cafe
12 | AppName={#MyAppName}
13 | AppVersion=${applicationVersion}
14 | AppVerName={#MyAppName} ${applicationVersion}
15 | AppPublisher={#MyAppPublisher}
16 | AppPublisherURL={#MyAppURL}
17 | AppSupportURL={#MyAppURL}
18 | AppUpdatesURL={#MyAppURL}
19 | AppCopyright=Copyright (C) 2021 Cafe Authors
20 |
21 | DefaultDirName={pf}\\cafe
22 | DisableDirPage=yes
23 | DefaultGroupName=Cafe
24 | DisableProgramGroupPage=yes
25 | ChangesEnvironment=yes
26 |
27 | LicenseFile=..\\..\\innosetup\\LICENSE.txt
28 |
29 | ; Uncomment the following line to run in non administrative install mode (install for current user only.)
30 | ;PrivilegesRequired=lowest
31 |
32 | SourceDir=..\\install\\cafe
33 | OutputDir=..\\..\\innosetup
34 | OutputBaseFilename=cafe-${applicationVersion}
35 |
36 | ; SetupIconFile=..\\..\\innosetup\\logo.ico
37 | Compression=lzma
38 | SolidCompression=yes
39 | WizardStyle=modern
40 |
41 | [Languages]
42 | Name: "english"; MessagesFile: "compiler:Default.isl"
43 |
44 | [Files]
45 | Source: "*"; DestDir: "{app}"; Flags: ignoreversion recursesubdirs createallsubdirs
46 | ; NOTE: Don't use "Flags: ignoreversion" on any shared system files
47 |
48 | [Tasks]
49 | Name: modifypath; Description: Add Cafe to PATH environment variable (recommended)
50 |
51 | [Registry]
52 | Root: HKLM; Subkey: "SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment"; \
53 | ValueType: expandsz; ValueName: "Path"; ValueData: "{olddata};{pf}\\cafe\\bin"; \
54 | Check: ModifyPathKeyCheck(ExpandConstant('{pf}\\cafe\\bin'));
55 |
56 | [Code]
57 | var ModifyPathKey: Boolean;
58 |
59 | function NextButtonClick(CurPageID: Integer): Boolean;
60 | begin
61 | if CurPageID = wpSelectTasks then
62 | begin
63 | ModifyPathKey := False;
64 |
65 | if IsTaskSelected('modifypath') then
66 | begin
67 | ModifyPathKey := True
68 | end;
69 | end;
70 | Result := True;
71 | end;
72 |
73 | function NeedsAddPath(Param: string): boolean;
74 | var
75 | OrigPath: string;
76 | begin
77 | if not RegQueryStringValue(HKEY_LOCAL_MACHINE,
78 | 'SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment',
79 | 'Path', OrigPath)
80 | then begin
81 | Result := True;
82 | exit;
83 | end;
84 | { look for the path with leading and trailing semicolon }
85 | { Pos() returns 0 if not found }
86 | Result := Pos(';' + Param + ';', ';' + OrigPath + ';') = 0;
87 | end;
88 |
89 | function ModifyPathKeyCheck(Path: string): Boolean;
90 | begin
91 | Result := True;
92 | Log(Path);
93 | if(ModifyPathKey) then
94 | begin
95 | Result := NeedsAddPath(Path);
96 | end;
97 | end;
98 |
--------------------------------------------------------------------------------
/gradle/innosetup/innoinstall.sh:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 | set -x
3 |
4 | rm -rf /tmp/inno
5 | mkdir /tmp/inno
6 | cd /tmp/inno
7 |
8 | wget -O is.exe http://files.jrsoftware.org/is/5/isetup-5.5.5.exe
9 | innoextract is.exe
10 | mkdir -p ~/".wine/drive_c/inno"
11 | cp -R app/* ~/".wine/drive_c/inno"
12 |
--------------------------------------------------------------------------------
/gradle/innosetup/iscc:
--------------------------------------------------------------------------------
1 | #!/bin/sh
2 | set -x
3 | unset DISPLAY
4 | scriptname=$1
5 | [ -f "$scriptname" ] && scriptname=$(winepath -w "$scriptname")
6 | wine "C:\inno\ISCC.exe" "$scriptname" "/q"
7 |
--------------------------------------------------------------------------------
/gradle/innosetup/logo.ico:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/cafe-jvm-lang/cafe/55714cc33eac061f5dca240f67a2b846108a6050/gradle/innosetup/logo.ico
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/cafe-jvm-lang/cafe/55714cc33eac061f5dca240f67a2b846108a6050/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #
2 | # Copyright (c) 2021. Dhyey Shah, Saurabh Pethani, Romil Nisar
3 | #
4 | # Developed by:
5 | # Dhyey Shah
6 | # https://github.com/dhyey-shah
7 | #
8 | # Contributors:
9 | # Saurabh Pethani
10 | # https://github.com/SaurabhPethani
11 | #
12 | # Romil Nisar
13 | #
14 | #
15 | # This file is part of Cafe.
16 | #
17 | # Cafe is free software: you can redistribute it and/or modify
18 | # it under the terms of the GNU General Public License as published by
19 | # the Free Software Foundation, version 3 of the License.
20 | #
21 | # Cafe is distributed in the hope that it will be useful,
22 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
23 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 | # GNU General Public License for more details.
25 | #
26 | # You should have received a copy of the GNU General Public License
27 | # along with Cafe. If not, see .
28 | #
29 | distributionBase=GRADLE_USER_HOME
30 | distributionPath=wrapper/dists
31 | distributionUrl=https\://services.gradle.org/distributions/gradle-6.4-bin.zip
32 | zipStoreBase=GRADLE_USER_HOME
33 | zipStorePath=wrapper/dists
34 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @rem
2 | @rem Copyright 2015 the original author or authors.
3 | @rem
4 | @rem Licensed under the Apache License, Version 2.0 (the "License");
5 | @rem you may not use this file except in compliance with the License.
6 | @rem You may obtain a copy of the License at
7 | @rem
8 | @rem https://www.apache.org/licenses/LICENSE-2.0
9 | @rem
10 | @rem Unless required by applicable law or agreed to in writing, software
11 | @rem distributed under the License is distributed on an "AS IS" BASIS,
12 | @rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | @rem See the License for the specific language governing permissions and
14 | @rem limitations under the License.
15 | @rem
16 |
17 | @if "%DEBUG%" == "" @echo off
18 | @rem ##########################################################################
19 | @rem
20 | @rem Gradle startup script for Windows
21 | @rem
22 | @rem ##########################################################################
23 |
24 | @rem Set local scope for the variables with windows NT shell
25 | if "%OS%"=="Windows_NT" setlocal
26 |
27 | set DIRNAME=%~dp0
28 | if "%DIRNAME%" == "" set DIRNAME=.
29 | set APP_BASE_NAME=%~n0
30 | set APP_HOME=%DIRNAME%
31 |
32 | @rem Resolve any "." and ".." in APP_HOME to make it shorter.
33 | for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi
34 |
35 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
36 | set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m"
37 |
38 | @rem Find java.exe
39 | if defined JAVA_HOME goto findJavaFromJavaHome
40 |
41 | set JAVA_EXE=java.exe
42 | %JAVA_EXE% -version >NUL 2>&1
43 | if "%ERRORLEVEL%" == "0" goto init
44 |
45 | echo.
46 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
47 | echo.
48 | echo Please set the JAVA_HOME variable in your environment to match the
49 | echo location of your Java installation.
50 |
51 | goto fail
52 |
53 | :findJavaFromJavaHome
54 | set JAVA_HOME=%JAVA_HOME:"=%
55 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
56 |
57 | if exist "%JAVA_EXE%" goto init
58 |
59 | echo.
60 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
61 | echo.
62 | echo Please set the JAVA_HOME variable in your environment to match the
63 | echo location of your Java installation.
64 |
65 | goto fail
66 |
67 | :init
68 | @rem Get command-line arguments, handling Windows variants
69 |
70 | if not "%OS%" == "Windows_NT" goto win9xME_args
71 |
72 | :win9xME_args
73 | @rem Slurp the command line arguments.
74 | set CMD_LINE_ARGS=
75 | set _SKIP=2
76 |
77 | :win9xME_args_slurp
78 | if "x%~1" == "x" goto execute
79 |
80 | set CMD_LINE_ARGS=%*
81 |
82 | :execute
83 | @rem Setup the command line
84 |
85 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
86 |
87 |
88 | @rem Execute Gradle
89 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
90 |
91 | :end
92 | @rem End local scope for the variables with windows NT shell
93 | if "%ERRORLEVEL%"=="0" goto mainEnd
94 |
95 | :fail
96 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
97 | rem the _cmd.exe /c_ return code!
98 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
99 | exit /b 1
100 |
101 | :mainEnd
102 | if "%OS%"=="Windows_NT" endlocal
103 |
104 | :omega
105 |
--------------------------------------------------------------------------------
/lib/asm-7.3.1.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/cafe-jvm-lang/cafe/55714cc33eac061f5dca240f67a2b846108a6050/lib/asm-7.3.1.jar
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2021. Dhyey Shah, Saurabh Pethani, Romil Nisar
3 | *
4 | * Developed by:
5 | * Dhyey Shah
6 | * https://github.com/dhyey-shah
7 | *
8 | * Contributors:
9 | * Saurabh Pethani
10 | * https://github.com/SaurabhPethani
11 | *
12 | * Romil Nisar
13 | *
14 | *
15 | * This file is part of Cafe.
16 | *
17 | * Cafe is free software: you can redistribute it and/or modify
18 | * it under the terms of the GNU General Public License as published by
19 | * the Free Software Foundation, version 3 of the License.
20 | *
21 | * Cafe is distributed in the hope that it will be useful,
22 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 | * GNU General Public License for more details.
25 | *
26 | * You should have received a copy of the GNU General Public License
27 | * along with Cafe. If not, see .
28 | */
29 |
30 | /*
31 | * This file was generated by the Gradle 'init' task.
32 | *
33 | * The settings file is used to specify which projects to include in your build.
34 | *
35 | * Detailed information about configuring a multi-project build in Gradle can be found
36 | * in the user manual at https://docs.gradle.org/6.4/userguide/multi_project_builds.html
37 | */
38 |
39 | rootProject.name = 'cafe'
40 |
--------------------------------------------------------------------------------
/src/main/java/compiler/Main.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2021. Dhyey Shah, Saurabh Pethani, Romil Nisar
3 | *
4 | * Developed by:
5 | * Dhyey Shah
6 | * https://github.com/dhyey-shah
7 | *
8 | * Contributors:
9 | * Saurabh Pethani
10 | * https://github.com/SaurabhPethani
11 | *
12 | * Romil Nisar
13 | *
14 | *
15 | * This file is part of Cafe.
16 | *
17 | * Cafe is free software: you can redistribute it and/or modify
18 | * it under the terms of the GNU General Public License as published by
19 | * the Free Software Foundation, version 3 of the License.
20 | *
21 | * Cafe is distributed in the hope that it will be useful,
22 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 | * GNU General Public License for more details.
25 | *
26 | * You should have received a copy of the GNU General Public License
27 | * along with Cafe. If not, see .
28 | */
29 |
30 | package compiler;
31 |
32 | public class Main {
33 |
34 | /**
35 | * Entry point into the compiler
36 | *
37 | * @param args expects a file-name.txt file
38 | */
39 | public static void main(String[] args) {
40 | System.exit(compile(args));
41 | }
42 |
43 | public static int compile(String[] args) {
44 | compiler.main.Main compiler = new compiler.main.Main();
45 | return compiler.compile(args).exitCode;
46 | }
47 | }
48 |
--------------------------------------------------------------------------------
/src/main/java/compiler/analyzer/Symbol.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2021. Dhyey Shah, Saurabh Pethani, Romil Nisar
3 | *
4 | * Developed by:
5 | * Dhyey Shah
6 | * https://github.com/dhyey-shah
7 | *
8 | * Contributors:
9 | * Saurabh Pethani
10 | * https://github.com/SaurabhPethani
11 | *
12 | * Romil Nisar
13 | *
14 | *
15 | * This file is part of Cafe.
16 | *
17 | * Cafe is free software: you can redistribute it and/or modify
18 | * it under the terms of the GNU General Public License as published by
19 | * the Free Software Foundation, version 3 of the License.
20 | *
21 | * Cafe is distributed in the hope that it will be useful,
22 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 | * GNU General Public License for more details.
25 | *
26 | * You should have received a copy of the GNU General Public License
27 | * along with Cafe. If not, see .
28 | */
29 |
30 | package compiler.analyzer;
31 |
32 | import java.util.Objects;
33 |
34 | public final class Symbol {
35 | String name;
36 | boolean isConst;
37 |
38 | public Symbol(String name) {
39 | this(name, false);
40 | }
41 |
42 | public Symbol(String name, boolean isConst) {
43 | this.name = name;
44 | this.isConst = isConst;
45 | }
46 |
47 | @Override
48 | public boolean equals(Object o) {
49 | if (this == o) return true;
50 | if (o == null || getClass() != o.getClass()) return false;
51 | Symbol symbol = (Symbol) o;
52 | return name.equals(symbol.name);
53 | }
54 |
55 | @Override
56 | public int hashCode() {
57 | return Objects.hash(name);
58 | }
59 | }
60 |
--------------------------------------------------------------------------------
/src/main/java/compiler/analyzer/SymbolTable.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2021. Dhyey Shah, Saurabh Pethani, Romil Nisar
3 | *
4 | * Developed by:
5 | * Dhyey Shah
6 | * https://github.com/dhyey-shah
7 | *
8 | * Contributors:
9 | * Saurabh Pethani
10 | * https://github.com/SaurabhPethani
11 | *
12 | * Romil Nisar
13 | *
14 | *
15 | * This file is part of Cafe.
16 | *
17 | * Cafe is free software: you can redistribute it and/or modify
18 | * it under the terms of the GNU General Public License as published by
19 | * the Free Software Foundation, version 3 of the License.
20 | *
21 | * Cafe is distributed in the hope that it will be useful,
22 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 | * GNU General Public License for more details.
25 | *
26 | * You should have received a copy of the GNU General Public License
27 | * along with Cafe. If not, see .
28 | */
29 |
30 | package compiler.analyzer;
31 |
32 | import java.util.HashMap;
33 | import java.util.Map;
34 |
35 | public class SymbolTable {
36 | public final SymbolTable parent;
37 |
38 | private final Map symbols;
39 |
40 | private boolean canOverrideParentDeclarations = true;
41 |
42 | public SymbolTable(SymbolTable parent) {
43 | this.parent = parent;
44 | symbols = new HashMap<>();
45 | }
46 |
47 | public SymbolTable dontOverrideParentDeclarations() {
48 | canOverrideParentDeclarations = false;
49 | return this;
50 | }
51 |
52 | public boolean insert(Symbol n) {
53 | if (!canOverrideParentDeclarations) {
54 | if (parent.isSymbolPresent(n.name)) {
55 | return false;
56 | }
57 | }
58 | if (symbols.containsKey(n.name))
59 | return false;
60 | else {
61 | symbols.put(n.name, n);
62 | return true;
63 | }
64 | }
65 |
66 | public boolean isSymbolPresent(String n) {
67 | SymbolTable table = this;
68 | while (table != null) {
69 | if (table.symbols.containsKey(n))
70 | return true;
71 | table = table.parent;
72 | }
73 | return false;
74 | }
75 |
76 | public boolean isSymbolConstant(String n) {
77 | SymbolTable table = this;
78 | while (table != null) {
79 | if (table.symbols.containsKey(n)) {
80 | if (table.symbols.get(n).isConst)
81 | return true;
82 | }
83 | table = table.parent;
84 | }
85 | return false;
86 | }
87 |
88 | // public Symbol get(String n){
89 | // SymbolTable table = this;
90 | // Symbol s = new Symbol(n);
91 | // while (table != null) {
92 | // if (table.symbols.contains(s))
93 | // ;
94 | // table = table.parent;
95 | // }
96 | // return false;
97 | // }
98 | }
99 |
--------------------------------------------------------------------------------
/src/main/java/compiler/ast/Tree.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2021. Dhyey Shah, Saurabh Pethani, Romil Nisar
3 | *
4 | * Developed by:
5 | * Dhyey Shah
6 | * https://github.com/dhyey-shah
7 | *
8 | * Contributors:
9 | * Saurabh Pethani
10 | * https://github.com/SaurabhPethani
11 | *
12 | * Romil Nisar
13 | *
14 | *
15 | * This file is part of Cafe.
16 | *
17 | * Cafe is free software: you can redistribute it and/or modify
18 | * it under the terms of the GNU General Public License as published by
19 | * the Free Software Foundation, version 3 of the License.
20 | *
21 | * Cafe is distributed in the hope that it will be useful,
22 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 | * GNU General Public License for more details.
25 | *
26 | * You should have received a copy of the GNU General Public License
27 | * along with Cafe. If not, see .
28 | */
29 |
30 | package compiler.ast;
31 |
32 | /**
33 | * The base contract for all tree nodes in AST.
34 | *
35 | * @author Dhyey
36 | */
37 | public interface Tree {
38 | void accept(TreeVisitor v);
39 | }
40 |
--------------------------------------------------------------------------------
/src/main/java/compiler/ast/TreeVisitor.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2021. Dhyey Shah, Saurabh Pethani, Romil Nisar
3 | *
4 | * Developed by:
5 | * Dhyey Shah
6 | * https://github.com/dhyey-shah
7 | *
8 | * Contributors:
9 | * Saurabh Pethani
10 | * https://github.com/SaurabhPethani
11 | *
12 | * Romil Nisar
13 | *
14 | *
15 | * This file is part of Cafe.
16 | *
17 | * Cafe is free software: you can redistribute it and/or modify
18 | * it under the terms of the GNU General Public License as published by
19 | * the Free Software Foundation, version 3 of the License.
20 | *
21 | * Cafe is distributed in the hope that it will be useful,
22 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 | * GNU General Public License for more details.
25 | *
26 | * You should have received a copy of the GNU General Public License
27 | * along with Cafe. If not, see .
28 | */
29 |
30 | package compiler.ast;
31 |
32 | public interface TreeVisitor {
33 | }
34 |
--------------------------------------------------------------------------------
/src/main/java/compiler/ast/package-info.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2021. Dhyey Shah, Saurabh Pethani, Romil Nisar
3 | *
4 | * Developed by:
5 | * Dhyey Shah
6 | * https://github.com/dhyey-shah
7 | *
8 | * Contributors:
9 | * Saurabh Pethani
10 | * https://github.com/SaurabhPethani
11 | *
12 | * Romil Nisar
13 | *
14 | *
15 | * This file is part of Cafe.
16 | *
17 | * Cafe is free software: you can redistribute it and/or modify
18 | * it under the terms of the GNU General Public License as published by
19 | * the Free Software Foundation, version 3 of the License.
20 | *
21 | * Cafe is distributed in the hope that it will be useful,
22 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 | * GNU General Public License for more details.
25 | *
26 | * You should have received a copy of the GNU General Public License
27 | * along with Cafe. If not, see .
28 | */
29 |
30 | package compiler.ast;
--------------------------------------------------------------------------------
/src/main/java/compiler/gen/JVMBytecodeUtils.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2021. Dhyey Shah, Saurabh Pethani, Romil Nisar
3 | *
4 | * Developed by:
5 | * Dhyey Shah
6 | * https://github.com/dhyey-shah
7 | *
8 | * Contributors:
9 | * Saurabh Pethani
10 | * https://github.com/SaurabhPethani
11 | *
12 | * Romil Nisar
13 | *
14 | *
15 | * This file is part of Cafe.
16 | *
17 | * Cafe is free software: you can redistribute it and/or modify
18 | * it under the terms of the GNU General Public License as published by
19 | * the Free Software Foundation, version 3 of the License.
20 | *
21 | * Cafe is distributed in the hope that it will be useful,
22 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 | * GNU General Public License for more details.
25 | *
26 | * You should have received a copy of the GNU General Public License
27 | * along with Cafe. If not, see .
28 | */
29 |
30 | package compiler.gen;
31 |
32 | import org.objectweb.asm.MethodVisitor;
33 |
34 | import static org.objectweb.asm.Opcodes.*;
35 |
36 | public class JVMBytecodeUtils {
37 | private JVMBytecodeUtils() {
38 | }
39 |
40 | static boolean between(int value, int lower, int upper) {
41 | return (value >= lower) && (value <= upper);
42 | }
43 |
44 | private static final int[] ICONST = {ICONST_M1, ICONST_0, ICONST_1, ICONST_2, ICONST_3, ICONST_4, ICONST_5};
45 |
46 | static void loadInteger(MethodVisitor methodVisitor, int value) {
47 | if (between(value, Short.MIN_VALUE, Short.MAX_VALUE)) {
48 | if (between(value, Byte.MIN_VALUE, Byte.MAX_VALUE)) {
49 | if (between(value, -1, 5)) {
50 | methodVisitor.visitInsn(ICONST[value + 1]);
51 | } else {
52 | methodVisitor.visitIntInsn(BIPUSH, value);
53 | }
54 | } else {
55 | methodVisitor.visitIntInsn(SIPUSH, value);
56 | }
57 | } else {
58 | methodVisitor.visitLdcInsn(value);
59 | }
60 | }
61 |
62 | static void loadLong(MethodVisitor methodVisitor, long value) {
63 | if (value == 0) {
64 | methodVisitor.visitInsn(LCONST_0);
65 | } else if (value == 1) {
66 | methodVisitor.visitInsn(LCONST_1);
67 | } else {
68 | methodVisitor.visitLdcInsn(value);
69 | }
70 | }
71 | }
72 |
--------------------------------------------------------------------------------
/src/main/java/compiler/ir/AnonymousFunction.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2021. Dhyey Shah, Saurabh Pethani, Romil Nisar
3 | *
4 | * Developed by:
5 | * Dhyey Shah
6 | * https://github.com/dhyey-shah
7 | *
8 | * Contributors:
9 | * Saurabh Pethani
10 | * https://github.com/SaurabhPethani
11 | *
12 | * Romil Nisar
13 | *
14 | *
15 | * This file is part of Cafe.
16 | *
17 | * Cafe is free software: you can redistribute it and/or modify
18 | * it under the terms of the GNU General Public License as published by
19 | * the Free Software Foundation, version 3 of the License.
20 | *
21 | * Cafe is distributed in the hope that it will be useful,
22 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 | * GNU General Public License for more details.
25 | *
26 | * You should have received a copy of the GNU General Public License
27 | * along with Cafe. If not, see .
28 | */
29 |
30 | package compiler.ir;
31 |
32 | import java.util.Collections;
33 | import java.util.List;
34 |
35 | public class AnonymousFunction extends ExpressionStatement {
36 | private TargetFuncWrapper targetFuncWrapper;
37 |
38 | private AnonymousFunction(TargetFuncWrapper targetFuncWrapper) {
39 | this.targetFuncWrapper = targetFuncWrapper;
40 | }
41 |
42 | public static AnonymousFunction func(TargetFuncWrapper targetFuncWrapper) {
43 | return new AnonymousFunction(targetFuncWrapper);
44 | }
45 |
46 | public CafeFunction getFunction() {
47 | return targetFuncWrapper.getTarget();
48 | }
49 |
50 | @Override
51 | public List> children() {
52 | if (targetFuncWrapper instanceof CafeClosure)
53 | return Collections.singletonList((CafeClosure) targetFuncWrapper);
54 | return Collections.singletonList((FunctionWrapper) targetFuncWrapper);
55 | }
56 |
57 | @Override
58 | protected AnonymousFunction self() {
59 | return this;
60 | }
61 |
62 | @Override
63 | public void accept(CafeIrVisitor visitor) {
64 | visitor.visitAnonymousFunction(this);
65 | }
66 | }
67 |
--------------------------------------------------------------------------------
/src/main/java/compiler/ir/AssignedStatement.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2021. Dhyey Shah, Saurabh Pethani, Romil Nisar
3 | *
4 | * Developed by:
5 | * Dhyey Shah
6 | * https://github.com/dhyey-shah
7 | *
8 | * Contributors:
9 | * Saurabh Pethani
10 | * https://github.com/SaurabhPethani
11 | *
12 | * Romil Nisar
13 | *
14 | *
15 | * This file is part of Cafe.
16 | *
17 | * Cafe is free software: you can redistribute it and/or modify
18 | * it under the terms of the GNU General Public License as published by
19 | * the Free Software Foundation, version 3 of the License.
20 | *
21 | * Cafe is distributed in the hope that it will be useful,
22 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 | * GNU General Public License for more details.
25 | *
26 | * You should have received a copy of the GNU General Public License
27 | * along with Cafe. If not, see .
28 | */
29 |
30 | package compiler.ir;
31 |
32 | public abstract class AssignedStatement extends ExpressionStatement {
33 | }
34 |
--------------------------------------------------------------------------------
/src/main/java/compiler/ir/AssignmentStatement.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2021. Dhyey Shah, Saurabh Pethani, Romil Nisar
3 | *
4 | * Developed by:
5 | * Dhyey Shah
6 | * https://github.com/dhyey-shah
7 | *
8 | * Contributors:
9 | * Saurabh Pethani
10 | * https://github.com/SaurabhPethani
11 | *
12 | * Romil Nisar
13 | *
14 | *
15 | * This file is part of Cafe.
16 | *
17 | * Cafe is free software: you can redistribute it and/or modify
18 | * it under the terms of the GNU General Public License as published by
19 | * the Free Software Foundation, version 3 of the License.
20 | *
21 | * Cafe is distributed in the hope that it will be useful,
22 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 | * GNU General Public License for more details.
25 | *
26 | * You should have received a copy of the GNU General Public License
27 | * along with Cafe. If not, see .
28 | */
29 |
30 | package compiler.ir;
31 |
32 | import java.util.LinkedList;
33 | import java.util.List;
34 |
35 | public class AssignmentStatement extends AssignedStatement {
36 |
37 | private ExpressionStatement> lhsExpression;
38 | private ExpressionStatement> rhsExpression;
39 |
40 | public static AssignmentStatement create(Object ref, Object value) {
41 | return new AssignmentStatement().to(ref)
42 | .as(value);
43 | }
44 |
45 | public AssignmentStatement to(Object ref) {
46 | if (ref == null) {
47 | throw new IllegalArgumentException("Must assign to a reference");
48 | }
49 | this.lhsExpression = ExpressionStatement.of(ref);
50 | return this;
51 | }
52 |
53 | public AssignmentStatement as(Object expr) {
54 |
55 | this.rhsExpression = ExpressionStatement.of(expr);
56 |
57 | return this;
58 | }
59 |
60 | public ExpressionStatement> getLhsExpression() {
61 | return lhsExpression;
62 | }
63 |
64 | public ExpressionStatement> getRhsExpression() {
65 | return rhsExpression;
66 | }
67 |
68 | @Override
69 | public List> children() {
70 | LinkedList> children = new LinkedList<>();
71 | //children.add(lhsExpression);
72 | children.add(rhsExpression);
73 | return children;
74 | }
75 |
76 | @Override
77 | protected AssignmentStatement self() {
78 | return this;
79 | }
80 |
81 | @Override
82 | public void accept(CafeIrVisitor visitor) {
83 | visitor.visitAssignment(this);
84 | }
85 |
86 | // @Override
87 | // public List getReferences() {
88 | // return List.of(symbolReference);
89 | // }
90 | }
91 |
--------------------------------------------------------------------------------
/src/main/java/compiler/ir/BinaryExpression.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2021. Dhyey Shah, Saurabh Pethani, Romil Nisar
3 | *
4 | * Developed by:
5 | * Dhyey Shah
6 | * https://github.com/dhyey-shah
7 | *
8 | * Contributors:
9 | * Saurabh Pethani
10 | * https://github.com/SaurabhPethani
11 | *
12 | * Romil Nisar
13 | *
14 | *
15 | * This file is part of Cafe.
16 | *
17 | * Cafe is free software: you can redistribute it and/or modify
18 | * it under the terms of the GNU General Public License as published by
19 | * the Free Software Foundation, version 3 of the License.
20 | *
21 | * Cafe is distributed in the hope that it will be useful,
22 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 | * GNU General Public License for more details.
25 | *
26 | * You should have received a copy of the GNU General Public License
27 | * along with Cafe. If not, see .
28 | */
29 |
30 | package compiler.ir;
31 |
32 | import java.util.Arrays;
33 | import java.util.List;
34 |
35 | public class BinaryExpression extends ExpressionStatement {
36 | private final OperatorType type;
37 | private ExpressionStatement> leftExpression;
38 | private ExpressionStatement> rightExpression;
39 |
40 | private BinaryExpression(OperatorType type) {
41 | this.type = type;
42 | }
43 |
44 | @Override
45 | protected BinaryExpression self() {
46 | return this;
47 | }
48 |
49 | public static BinaryExpression of(Object type) {
50 | return new BinaryExpression(OperatorType.of(type));
51 | }
52 |
53 | public BinaryExpression left(Object expr) {
54 | this.leftExpression = ExpressionStatement.of(expr);
55 | return this;
56 | }
57 |
58 | public BinaryExpression right(Object expr) {
59 | this.rightExpression = ExpressionStatement.of(expr);
60 | return this;
61 | }
62 |
63 | public ExpressionStatement> right() {
64 | return rightExpression;
65 | }
66 |
67 | public ExpressionStatement> left() {
68 | return leftExpression;
69 | }
70 |
71 | public OperatorType getType() {
72 | return type;
73 | }
74 |
75 | @Override
76 | public List> children() {
77 | return Arrays.asList(leftExpression, rightExpression);
78 | }
79 |
80 | @Override
81 | public String toString() {
82 | return "BinaryExpression{" +
83 | "type=" + type +
84 | ", leftExpression=" + leftExpression +
85 | ", rightExpression=" + rightExpression +
86 | '}';
87 | }
88 |
89 | @Override
90 | public void accept(CafeIrVisitor visitor) {
91 | visitor.visitBinaryExpression(this);
92 | }
93 | }
94 |
--------------------------------------------------------------------------------
/src/main/java/compiler/ir/Block.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2021. Dhyey Shah, Saurabh Pethani, Romil Nisar
3 | *
4 | * Developed by:
5 | * Dhyey Shah
6 | * https://github.com/dhyey-shah
7 | *
8 | * Contributors:
9 | * Saurabh Pethani
10 | * https://github.com/SaurabhPethani
11 | *
12 | * Romil Nisar
13 | *
14 | *
15 | * This file is part of Cafe.
16 | *
17 | * Cafe is free software: you can redistribute it and/or modify
18 | * it under the terms of the GNU General Public License as published by
19 | * the Free Software Foundation, version 3 of the License.
20 | *
21 | * Cafe is distributed in the hope that it will be useful,
22 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 | * GNU General Public License for more details.
25 | *
26 | * You should have received a copy of the GNU General Public License
27 | * along with Cafe. If not, see .
28 | */
29 |
30 | package compiler.ir;
31 |
32 | import java.util.Collections;
33 | import java.util.LinkedList;
34 | import java.util.List;
35 |
36 | public class Block extends ExpressionStatement {
37 | private final List> statements = new LinkedList<>();
38 | private ReferenceTable referenceTable;
39 | private boolean hasReturn = false;
40 |
41 | private Block(ReferenceTable referenceTable) {
42 | this.referenceTable = referenceTable;
43 | }
44 |
45 | public static Block create(ReferenceTable referenceTable) {
46 | return new Block(referenceTable);
47 | }
48 |
49 | public ReferenceTable getReferenceTable() {
50 | return referenceTable;
51 | }
52 |
53 | public List> getStatements() {
54 | return statements;
55 | }
56 |
57 | @Override
58 | protected Block self() {
59 | return this;
60 | }
61 |
62 | public static Block empty() {
63 | return new Block(new ReferenceTable());
64 | }
65 |
66 | public Block add(Object statement) {
67 | if (statement != null)
68 | this.addStatement(CafeStatement.of(statement));
69 | return this;
70 | }
71 |
72 | private void addStatement(CafeStatement> statement) {
73 | statements.add(statement);
74 | updateStateWith(statement);
75 | }
76 |
77 | private void updateStateWith(CafeStatement> statement) {
78 | checkForReturns(statement);
79 | }
80 |
81 | public static Block of(Object block) {
82 | if (block == null) {
83 | return empty();
84 | }
85 | if (block instanceof Block) {
86 | return (Block) block;
87 | }
88 | if (block instanceof CafeStatement>) {
89 | return empty().add(block);
90 | }
91 | throw cantConvert("Block", block);
92 | }
93 |
94 |
95 | public boolean hasReturn() {
96 | return hasReturn;
97 | }
98 |
99 | private void checkForReturns(CafeStatement> statement) {
100 | if (statement instanceof ReturnStatement)
101 | hasReturn = true;
102 | }
103 |
104 | @Override
105 | public List> children() {
106 | return Collections.unmodifiableList(statements);
107 | }
108 |
109 | @Override
110 | public void accept(CafeIrVisitor visitor) {
111 | visitor.visitBlock(this);
112 | }
113 | }
114 |
--------------------------------------------------------------------------------
/src/main/java/compiler/ir/BreakContinueStatement.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2021. Dhyey Shah, Saurabh Pethani, Romil Nisar
3 | *
4 | * Developed by:
5 | * Dhyey Shah
6 | * https://github.com/dhyey-shah
7 | *
8 | * Contributors:
9 | * Saurabh Pethani
10 | * https://github.com/SaurabhPethani
11 | *
12 | * Romil Nisar
13 | *
14 | *
15 | * This file is part of Cafe.
16 | *
17 | * Cafe is free software: you can redistribute it and/or modify
18 | * it under the terms of the GNU General Public License as published by
19 | * the Free Software Foundation, version 3 of the License.
20 | *
21 | * Cafe is distributed in the hope that it will be useful,
22 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 | * GNU General Public License for more details.
25 | *
26 | * You should have received a copy of the GNU General Public License
27 | * along with Cafe. If not, see .
28 | */
29 |
30 | package compiler.ir;
31 |
32 | public class BreakContinueStatement extends CafeStatement {
33 | public enum Type {
34 | BREAK, CONTINUE
35 | }
36 |
37 | private final Type type;
38 | private ForLoopStatement enclosingLoop;
39 |
40 | private BreakContinueStatement(Type type) {
41 | this.type = type;
42 | }
43 |
44 | public static BreakContinueStatement newContinue() {
45 | return new BreakContinueStatement(Type.CONTINUE);
46 | }
47 |
48 | public static BreakContinueStatement newBreak() {
49 | return new BreakContinueStatement(Type.BREAK);
50 | }
51 |
52 | public BreakContinueStatement setEnclosingLoop(ForLoopStatement enclosingLoop) {
53 | this.enclosingLoop = enclosingLoop;
54 | return this;
55 | }
56 |
57 | public ForLoopStatement getEnclosingLoop() {
58 | return enclosingLoop;
59 | }
60 |
61 | public Type getType() {
62 | return type;
63 | }
64 |
65 | @Override
66 | protected BreakContinueStatement self() {
67 | return this;
68 | }
69 |
70 | @Override
71 | public void accept(CafeIrVisitor visitor) {
72 | visitor.visitBreakContinue(this);
73 | }
74 | }
75 |
--------------------------------------------------------------------------------
/src/main/java/compiler/ir/CafeClosure.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2021. Dhyey Shah, Saurabh Pethani, Romil Nisar
3 | *
4 | * Developed by:
5 | * Dhyey Shah
6 | * https://github.com/dhyey-shah
7 | *
8 | * Contributors:
9 | * Saurabh Pethani
10 | * https://github.com/SaurabhPethani
11 | *
12 | * Romil Nisar
13 | *
14 | *
15 | * This file is part of Cafe.
16 | *
17 | * Cafe is free software: you can redistribute it and/or modify
18 | * it under the terms of the GNU General Public License as published by
19 | * the Free Software Foundation, version 3 of the License.
20 | *
21 | * Cafe is distributed in the hope that it will be useful,
22 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 | * GNU General Public License for more details.
25 | *
26 | * You should have received a copy of the GNU General Public License
27 | * along with Cafe. If not, see .
28 | */
29 |
30 | package compiler.ir;
31 |
32 | import java.util.List;
33 |
34 | public class CafeClosure extends ExpressionStatement implements TargetFuncWrapper {
35 | private CafeFunction target;
36 |
37 | public CafeClosure(CafeFunction target) {
38 | this.target = target;
39 | }
40 |
41 | @Override
42 | public CafeFunction getTarget() {
43 | return target;
44 | }
45 |
46 | public List getClosureReferences() {
47 | return target.getClosureParameterNames();
48 | }
49 |
50 | public int getClosureReferencesSize() {
51 | return target.getClosureParameterNames()
52 | .size();
53 | }
54 |
55 | @Override
56 | protected CafeClosure self() {
57 | return this;
58 | }
59 |
60 | @Override
61 | public void accept(CafeIrVisitor visitor) {
62 | visitor.visitClosure(this);
63 | }
64 | }
65 |
--------------------------------------------------------------------------------
/src/main/java/compiler/ir/CafeElement.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2021. Dhyey Shah, Saurabh Pethani, Romil Nisar
3 | *
4 | * Developed by:
5 | * Dhyey Shah
6 | * https://github.com/dhyey-shah
7 | *
8 | * Contributors:
9 | * Saurabh Pethani
10 | * https://github.com/SaurabhPethani
11 | *
12 | * Romil Nisar
13 | *
14 | *
15 | * This file is part of Cafe.
16 | *
17 | * Cafe is free software: you can redistribute it and/or modify
18 | * it under the terms of the GNU General Public License as published by
19 | * the Free Software Foundation, version 3 of the License.
20 | *
21 | * Cafe is distributed in the hope that it will be useful,
22 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 | * GNU General Public License for more details.
25 | *
26 | * You should have received a copy of the GNU General Public License
27 | * along with Cafe. If not, see .
28 | */
29 |
30 | package compiler.ir;
31 |
32 | import java.util.Collections;
33 | import java.util.List;
34 |
35 | public abstract class CafeElement> {
36 | private CafeElement> parent;
37 |
38 | protected abstract T self();
39 |
40 | private void setParent(CafeElement> parent) {
41 | this.parent = parent;
42 | }
43 |
44 | public final CafeElement> parent() {
45 | return this.parent;
46 | }
47 |
48 | public abstract void accept(CafeIrVisitor visitor);
49 |
50 | public List> children() {
51 | return Collections.emptyList();
52 | }
53 |
54 | protected static final RuntimeException cantConvert(String expected, Object value) {
55 | return new ClassCastException(String.format(
56 | "expecting a %s but got a %s",
57 | expected,
58 | value == null ? "null value" : value.getClass()
59 | .getName()));
60 | }
61 |
62 | public void walk(CafeIrVisitor visitor) {
63 | for (CafeElement> e : children())
64 | e.accept(visitor);
65 | }
66 | }
67 |
--------------------------------------------------------------------------------
/src/main/java/compiler/ir/CafeExport.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2021. Dhyey Shah, Saurabh Pethani, Romil Nisar
3 | *
4 | * Developed by:
5 | * Dhyey Shah
6 | * https://github.com/dhyey-shah
7 | *
8 | * Contributors:
9 | * Saurabh Pethani
10 | * https://github.com/SaurabhPethani
11 | *
12 | * Romil Nisar
13 | *
14 | *
15 | * This file is part of Cafe.
16 | *
17 | * Cafe is free software: you can redistribute it and/or modify
18 | * it under the terms of the GNU General Public License as published by
19 | * the Free Software Foundation, version 3 of the License.
20 | *
21 | * Cafe is distributed in the hope that it will be useful,
22 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 | * GNU General Public License for more details.
25 | *
26 | * You should have received a copy of the GNU General Public License
27 | * along with Cafe. If not, see .
28 | */
29 |
30 | package compiler.ir;
31 |
32 | import java.util.Objects;
33 |
34 | public class CafeExport extends CafeStatement {
35 | private String name;
36 |
37 | private CafeExport(String name) {
38 | this.name = name;
39 | }
40 |
41 | public static CafeExport export(String name) {
42 | return new CafeExport(name);
43 | }
44 |
45 | public String getName() {
46 | return name;
47 | }
48 |
49 | @Override
50 | public boolean equals(Object o) {
51 | if (this == o) return true;
52 | if (o == null || getClass() != o.getClass()) return false;
53 | CafeExport export = (CafeExport) o;
54 | return Objects.equals(name, export.name);
55 | }
56 |
57 | @Override
58 | public int hashCode() {
59 | return Objects.hash(name);
60 | }
61 |
62 | @Override
63 | protected CafeExport self() {
64 | return this;
65 | }
66 |
67 | @Override
68 | public void accept(CafeIrVisitor visitor) {
69 | visitor.visitCafeExport(this);
70 | }
71 | }
72 |
--------------------------------------------------------------------------------
/src/main/java/compiler/ir/CafeImport.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2021. Dhyey Shah, Saurabh Pethani, Romil Nisar
3 | *
4 | * Developed by:
5 | * Dhyey Shah
6 | * https://github.com/dhyey-shah
7 | *
8 | * Contributors:
9 | * Saurabh Pethani
10 | * https://github.com/SaurabhPethani
11 | *
12 | * Romil Nisar
13 | *
14 | *
15 | * This file is part of Cafe.
16 | *
17 | * Cafe is free software: you can redistribute it and/or modify
18 | * it under the terms of the GNU General Public License as published by
19 | * the Free Software Foundation, version 3 of the License.
20 | *
21 | * Cafe is distributed in the hope that it will be useful,
22 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 | * GNU General Public License for more details.
25 | *
26 | * You should have received a copy of the GNU General Public License
27 | * along with Cafe. If not, see .
28 | */
29 |
30 | package compiler.ir;
31 |
32 | import java.util.HashMap;
33 | import java.util.Map;
34 |
35 | public class CafeImport extends CafeStatement {
36 | private final Map nameAlias;
37 | private final String modulePath;
38 | private boolean isDefault = false;
39 |
40 | private CafeImport(String modulePath) {
41 | nameAlias = new HashMap<>();
42 | this.modulePath = modulePath;
43 | }
44 |
45 | public static CafeImport of(String moduleName) {
46 | return new CafeImport(moduleName);
47 | }
48 |
49 | public void add(String name, String alias) {
50 | nameAlias.put(name, alias);
51 | }
52 |
53 | public Map getNameAlias() {
54 | return nameAlias;
55 | }
56 |
57 | public CafeImport asDefault() {
58 | isDefault = true;
59 | return this;
60 | }
61 |
62 | public boolean isDefault() {
63 | return isDefault;
64 | }
65 |
66 | public String getModulePath() {
67 | return modulePath;
68 | }
69 |
70 | @Override
71 | protected CafeImport self() {
72 | return this;
73 | }
74 |
75 | @Override
76 | public void accept(CafeIrVisitor visitor) {
77 | visitor.visitCafeImport(this);
78 | }
79 |
80 | }
81 |
--------------------------------------------------------------------------------
/src/main/java/compiler/ir/CafeIrVisitor.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2021. Dhyey Shah, Saurabh Pethani, Romil Nisar
3 | *
4 | * Developed by:
5 | * Dhyey Shah
6 | * https://github.com/dhyey-shah
7 | *
8 | * Contributors:
9 | * Saurabh Pethani
10 | * https://github.com/SaurabhPethani
11 | *
12 | * Romil Nisar
13 | *
14 | *
15 | * This file is part of Cafe.
16 | *
17 | * Cafe is free software: you can redistribute it and/or modify
18 | * it under the terms of the GNU General Public License as published by
19 | * the Free Software Foundation, version 3 of the License.
20 | *
21 | * Cafe is distributed in the hope that it will be useful,
22 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 | * GNU General Public License for more details.
25 | *
26 | * You should have received a copy of the GNU General Public License
27 | * along with Cafe. If not, see .
28 | */
29 |
30 | package compiler.ir;
31 |
32 | public interface CafeIrVisitor {
33 | void visitModule(CafeModule module);
34 |
35 | void visitSymbolReference(SymbolReference symbolReference);
36 |
37 | void visitBlock(Block block);
38 |
39 | void visitFunction(CafeFunction cafeFunction);
40 |
41 | void visitAssignment(AssignmentStatement assignmentStatement);
42 |
43 | void visitBinaryExpression(BinaryExpression binaryExpression);
44 |
45 | void visitUnaryExpression(UnaryExpression unaryExpression);
46 |
47 | void visitConstantStatement(ConstantStatement constantStatement);
48 |
49 | void visitReferenceLookup(ReferenceLookup referenceLookup);
50 |
51 | void visitFunctionWrapper(FunctionWrapper functionWrapper);
52 |
53 | void visitDeclarativeAssignment(DeclarativeAssignmentStatement declarativeAssignmentStatement);
54 |
55 | void visitObjectAccess(ObjectAccessStatement objectAccessStatement);
56 |
57 | void visitMethodInvocation(MethodInvocation methodInvocation);
58 |
59 | void visitFunctionInvocation(FunctionInvocation functionInvocation);
60 |
61 | void visitNull(NullStatement aNull);
62 |
63 | void visitSubscript(SubscriptExpression subscriptExpression);
64 |
65 | void visitPropertyAccess(PropertyAccess propertyAccess);
66 |
67 | void visitReturn(ReturnStatement returnStatement);
68 |
69 | void visitThis(ThisStatement thisStatement);
70 |
71 | void visitObjectCreation(ObjectCreationStatement creationStatement);
72 |
73 | void visitConditionalBranching(ConditionalBranching conditionalBranching);
74 |
75 | void visitForLoop(ForLoopStatement forLoopStatement);
76 |
77 | void visitCafeImport(CafeImport cafeImport);
78 |
79 | void visitBreakContinue(BreakContinueStatement breakContinueStatement);
80 |
81 | void visitCafeExport(CafeExport cafeExport);
82 |
83 | void visitClosure(CafeClosure cafeClosure);
84 |
85 | void visitAnonymousFunction(AnonymousFunction anonymousFunction);
86 |
87 | void visitListCollection(ListCollection listCollection);
88 |
89 | void visitSlice(SliceExpression sliceExpression);
90 | }
91 |
--------------------------------------------------------------------------------
/src/main/java/compiler/ir/CafeStatement.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2021. Dhyey Shah, Saurabh Pethani, Romil Nisar
3 | *
4 | * Developed by:
5 | * Dhyey Shah
6 | * https://github.com/dhyey-shah
7 | *
8 | * Contributors:
9 | * Saurabh Pethani
10 | * https://github.com/SaurabhPethani
11 | *
12 | * Romil Nisar
13 | *
14 | *
15 | * This file is part of Cafe.
16 | *
17 | * Cafe is free software: you can redistribute it and/or modify
18 | * it under the terms of the GNU General Public License as published by
19 | * the Free Software Foundation, version 3 of the License.
20 | *
21 | * Cafe is distributed in the hope that it will be useful,
22 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 | * GNU General Public License for more details.
25 | *
26 | * You should have received a copy of the GNU General Public License
27 | * along with Cafe. If not, see .
28 | */
29 |
30 | package compiler.ir;
31 |
32 | public abstract class CafeStatement> extends CafeElement {
33 |
34 | public static CafeStatement> of(Object statement) {
35 | // TODO: return null ?, not sure
36 | if (statement == null) return null;
37 | if (statement instanceof CafeStatement) {
38 | return (CafeStatement) statement;
39 | }
40 | throw cantConvert("CafeStatement", statement);
41 | }
42 |
43 | }
44 |
--------------------------------------------------------------------------------
/src/main/java/compiler/ir/ConstantStatement.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2021. Dhyey Shah, Saurabh Pethani, Romil Nisar
3 | *
4 | * Developed by:
5 | * Dhyey Shah
6 | * https://github.com/dhyey-shah
7 | *
8 | * Contributors:
9 | * Saurabh Pethani
10 | * https://github.com/SaurabhPethani
11 | *
12 | * Romil Nisar
13 | *
14 | *
15 | * This file is part of Cafe.
16 | *
17 | * Cafe is free software: you can redistribute it and/or modify
18 | * it under the terms of the GNU General Public License as published by
19 | * the Free Software Foundation, version 3 of the License.
20 | *
21 | * Cafe is distributed in the hope that it will be useful,
22 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 | * GNU General Public License for more details.
25 | *
26 | * You should have received a copy of the GNU General Public License
27 | * along with Cafe. If not, see .
28 | */
29 |
30 | package compiler.ir;
31 |
32 | public class ConstantStatement extends ExpressionStatement {
33 | private Object value;
34 |
35 | public ConstantStatement(Object o) {
36 | this.value = o;
37 | }
38 |
39 | @Override
40 | protected ConstantStatement self() {
41 | return this;
42 | }
43 |
44 | public static ConstantStatement of(Object o) {
45 | if (o instanceof ConstantStatement)
46 | return (ConstantStatement) o;
47 |
48 | if (!isLiteralValue(o)) {
49 | throw new IllegalArgumentException("Not a constant value: " + o);
50 | }
51 | return new ConstantStatement(o);
52 | }
53 |
54 | public static boolean isLiteralValue(Object v) {
55 | return v == null
56 | || v instanceof String
57 | || v instanceof Character
58 | || v instanceof Number
59 | || v instanceof Boolean
60 | ;
61 | }
62 |
63 | public Object value() {
64 | return value;
65 | }
66 |
67 | @Override
68 | public String toString() {
69 | return "Const{" +
70 | value +
71 | '}';
72 | }
73 |
74 | @Override
75 | public void accept(CafeIrVisitor visitor) {
76 | visitor.visitConstantStatement(this);
77 | }
78 | }
79 |
--------------------------------------------------------------------------------
/src/main/java/compiler/ir/DeclarativeAssignmentStatement.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2021. Dhyey Shah, Saurabh Pethani, Romil Nisar
3 | *
4 | * Developed by:
5 | * Dhyey Shah
6 | * https://github.com/dhyey-shah
7 | *
8 | * Contributors:
9 | * Saurabh Pethani
10 | * https://github.com/SaurabhPethani
11 | *
12 | * Romil Nisar
13 | *
14 | *
15 | * This file is part of Cafe.
16 | *
17 | * Cafe is free software: you can redistribute it and/or modify
18 | * it under the terms of the GNU General Public License as published by
19 | * the Free Software Foundation, version 3 of the License.
20 | *
21 | * Cafe is distributed in the hope that it will be useful,
22 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 | * GNU General Public License for more details.
25 | *
26 | * You should have received a copy of the GNU General Public License
27 | * along with Cafe. If not, see .
28 | */
29 |
30 | package compiler.ir;
31 |
32 | import java.util.Collections;
33 | import java.util.List;
34 |
35 | public class DeclarativeAssignmentStatement extends AssignedStatement {
36 |
37 | private SymbolReference symbolReference;
38 | private ExpressionStatement> expressionStatement;
39 | private boolean isAssigned = true;
40 |
41 | private DeclarativeAssignmentStatement() {
42 | }
43 |
44 | public static DeclarativeAssignmentStatement create(SymbolReference ref, Object expr) {
45 | return new DeclarativeAssignmentStatement().to(ref)
46 | .as(expr);
47 | }
48 |
49 | public DeclarativeAssignmentStatement to(SymbolReference ref) {
50 | symbolReference = ref;
51 | return this;
52 | }
53 |
54 | public DeclarativeAssignmentStatement as(Object value) {
55 | if (value == null) {
56 | expressionStatement = new NullStatement();
57 | isAssigned = false;
58 | } else
59 | expressionStatement = ExpressionStatement.of(value);
60 | return this;
61 | }
62 |
63 | public SymbolReference getSymbolReference() {
64 | return symbolReference;
65 | }
66 |
67 | @Override
68 | public List> children() {
69 | return Collections.singletonList(expressionStatement);
70 | }
71 |
72 | @Override
73 | protected DeclarativeAssignmentStatement self() {
74 | return this;
75 | }
76 |
77 | @Override
78 | public void accept(CafeIrVisitor visitor) {
79 | visitor.visitDeclarativeAssignment(this);
80 | }
81 | }
82 |
--------------------------------------------------------------------------------
/src/main/java/compiler/ir/ExpressionStatement.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2021. Dhyey Shah, Saurabh Pethani, Romil Nisar
3 | *
4 | * Developed by:
5 | * Dhyey Shah
6 | * https://github.com/dhyey-shah
7 | *
8 | * Contributors:
9 | * Saurabh Pethani
10 | * https://github.com/SaurabhPethani
11 | *
12 | * Romil Nisar
13 | *
14 | *
15 | * This file is part of Cafe.
16 | *
17 | * Cafe is free software: you can redistribute it and/or modify
18 | * it under the terms of the GNU General Public License as published by
19 | * the Free Software Foundation, version 3 of the License.
20 | *
21 | * Cafe is distributed in the hope that it will be useful,
22 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 | * GNU General Public License for more details.
25 | *
26 | * You should have received a copy of the GNU General Public License
27 | * along with Cafe. If not, see .
28 | */
29 |
30 | package compiler.ir;
31 |
32 | public abstract class ExpressionStatement> extends CafeStatement {
33 | public static ExpressionStatement> of(Object expr) {
34 | if (expr == null)
35 | return null;
36 | if (expr instanceof ExpressionStatement)
37 | return (ExpressionStatement>) expr;
38 |
39 | throw cantConvert("ExpressionStatement", expr);
40 | }
41 | }
42 |
--------------------------------------------------------------------------------
/src/main/java/compiler/ir/ForLoopStatement.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2021. Dhyey Shah, Saurabh Pethani, Romil Nisar
3 | *
4 | * Developed by:
5 | * Dhyey Shah
6 | * https://github.com/dhyey-shah
7 | *
8 | * Contributors:
9 | * Saurabh Pethani
10 | * https://github.com/SaurabhPethani
11 | *
12 | * Romil Nisar
13 | *
14 | *
15 | * This file is part of Cafe.
16 | *
17 | * Cafe is free software: you can redistribute it and/or modify
18 | * it under the terms of the GNU General Public License as published by
19 | * the Free Software Foundation, version 3 of the License.
20 | *
21 | * Cafe is distributed in the hope that it will be useful,
22 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 | * GNU General Public License for more details.
25 | *
26 | * You should have received a copy of the GNU General Public License
27 | * along with Cafe. If not, see .
28 | */
29 |
30 | package compiler.ir;
31 |
32 | import java.util.List;
33 |
34 | public class ForLoopStatement extends CafeStatement {
35 | private List initStatement = null;
36 | private ExpressionStatement> condition;
37 | private List> postStatement = null;
38 | private Block block = null;
39 |
40 | private ForLoopStatement() {
41 | }
42 |
43 | public static ForLoopStatement loop() {
44 | return new ForLoopStatement();
45 | }
46 |
47 | public ForLoopStatement init(List list) {
48 | initStatement = list;
49 | return this;
50 | }
51 |
52 | public ForLoopStatement condition(ExpressionStatement> expression) {
53 | condition = expression;
54 | return this;
55 | }
56 |
57 | public ForLoopStatement block(Block block) {
58 | this.block = block;
59 | return this;
60 | }
61 |
62 | public ForLoopStatement postStatement(List> post) {
63 | postStatement = post;
64 | return this;
65 | }
66 |
67 | public List getInitStatements() {
68 | return initStatement;
69 | }
70 |
71 | public Block getBlock() {
72 | return block;
73 | }
74 |
75 | public List> getPostStatements() {
76 | return postStatement;
77 | }
78 |
79 | public ExpressionStatement> getCondition() {
80 | return condition;
81 | }
82 |
83 | public boolean hasInitStatement() {
84 | if (initStatement != null)
85 | return true;
86 | return false;
87 | }
88 |
89 | public boolean hasPostStatement() {
90 | if (postStatement != null)
91 | return true;
92 | return false;
93 | }
94 |
95 | @Override
96 | protected ForLoopStatement self() {
97 | return this;
98 | }
99 |
100 | @Override
101 | public void accept(CafeIrVisitor visitor) {
102 | visitor.visitForLoop(this);
103 | }
104 | }
105 |
--------------------------------------------------------------------------------
/src/main/java/compiler/ir/FunctionInvocation.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2021. Dhyey Shah, Saurabh Pethani, Romil Nisar
3 | *
4 | * Developed by:
5 | * Dhyey Shah
6 | * https://github.com/dhyey-shah
7 | *
8 | * Contributors:
9 | * Saurabh Pethani
10 | * https://github.com/SaurabhPethani
11 | *
12 | * Romil Nisar
13 | *
14 | *
15 | * This file is part of Cafe.
16 | *
17 | * Cafe is free software: you can redistribute it and/or modify
18 | * it under the terms of the GNU General Public License as published by
19 | * the Free Software Foundation, version 3 of the License.
20 | *
21 | * Cafe is distributed in the hope that it will be useful,
22 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 | * GNU General Public License for more details.
25 | *
26 | * You should have received a copy of the GNU General Public License
27 | * along with Cafe. If not, see .
28 | */
29 |
30 | package compiler.ir;
31 |
32 | import java.util.LinkedList;
33 | import java.util.List;
34 |
35 | public class FunctionInvocation extends ExpressionStatement {
36 | private String name;
37 | private ExpressionStatement> ref;
38 | private List> arguments;
39 |
40 | private FunctionInvocation(String name, ExpressionStatement> ref, List> arguments) {
41 | this.name = name;
42 | this.ref = ref;
43 | this.arguments = arguments;
44 | }
45 |
46 | public ExpressionStatement> getReference() {
47 | return ref;
48 | }
49 |
50 | public static FunctionInvocation create(Object ref, List