├── .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 | [![Build](https://github.com/cafe-jvm-lang/cafe/workflows/Build/badge.svg)](https://github.com/cafe-jvm-lang/cafe/actions?query=workflow%3ABuild) 4 | [![License: GPL v3](https://img.shields.io/badge/License-GPLv3-blue.svg)](https://www.gnu.org/licenses/gpl-3.0) 5 | [![Github All Releases](https://img.shields.io/github/downloads/cafe-jvm-lang/cafe/total.svg)](#) 6 | [![Join the chat at https://gitter.im/cafe-jvm-lang/cafe-lang](https://badges.gitter.im/cafe-jvm-lang/cafe-lang.svg)](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 args) { 51 | List> arguments = new LinkedList<>(); 52 | for (Object arg : args) { 53 | if (arg instanceof CafeElement) { 54 | arguments.add((CafeElement) arg); 55 | } else { 56 | arguments.add(ExpressionStatement.of(arg)); 57 | } 58 | } 59 | String name = "#_ANN_CALL"; 60 | if (ref instanceof ReferenceLookup) 61 | name = ((ReferenceLookup) ref).getName(); 62 | return new FunctionInvocation( 63 | name, 64 | ExpressionStatement.of(ref), 65 | arguments 66 | ); 67 | } 68 | 69 | public String getName() { 70 | return name; 71 | } 72 | 73 | public int getArity() { 74 | return arguments.size(); 75 | } 76 | 77 | public List> getArguments() { 78 | return arguments; 79 | } 80 | 81 | @Override 82 | protected FunctionInvocation self() { 83 | return this; 84 | } 85 | 86 | @Override 87 | public void accept(CafeIrVisitor visitor) { 88 | visitor.visitFunctionInvocation(this); 89 | } 90 | 91 | @Override 92 | public String toString() { 93 | return "FunctionInvocation{" + 94 | "name='" + name + '\'' + 95 | ", arguments=" + arguments + 96 | '}'; 97 | } 98 | } 99 | -------------------------------------------------------------------------------- /src/main/java/compiler/ir/FunctionWrapper.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 FunctionWrapper extends ExpressionStatement implements TargetFuncWrapper { 36 | private CafeFunction target; 37 | 38 | private FunctionWrapper(CafeFunction function) { 39 | this.target = function; 40 | } 41 | 42 | public static FunctionWrapper wrap(CafeFunction target) { 43 | return new FunctionWrapper(target); 44 | } 45 | 46 | @Override 47 | public CafeFunction getTarget() { 48 | return target; 49 | } 50 | 51 | @Override 52 | public List> children() { 53 | return Collections.singletonList(target); 54 | } 55 | 56 | @Override 57 | protected FunctionWrapper self() { 58 | return this; 59 | } 60 | 61 | @Override 62 | public void accept(CafeIrVisitor visitor) { 63 | visitor.visitFunctionWrapper(this); 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /src/main/java/compiler/ir/ListCollection.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 ListCollection extends ExpressionStatement { 36 | private List> items = new LinkedList<>(); 37 | private int index = -1; 38 | 39 | private ListCollection() { 40 | } 41 | 42 | public static ListCollection list() { 43 | return new ListCollection(); 44 | } 45 | 46 | public void add(ExpressionStatement expr) { 47 | items.add(expr); 48 | } 49 | 50 | public List> getItems() { 51 | return items; 52 | } 53 | 54 | public void setIndex(int index) { 55 | this.index = index; 56 | } 57 | 58 | public int index() { 59 | return index; 60 | } 61 | 62 | @Override 63 | public List> children() { 64 | return new LinkedList<>(items); 65 | } 66 | 67 | @Override 68 | protected ListCollection self() { 69 | return this; 70 | } 71 | 72 | @Override 73 | public void accept(CafeIrVisitor visitor) { 74 | visitor.visitListCollection(this); 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /src/main/java/compiler/ir/MethodInvocation.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 MethodInvocation extends ExpressionStatement { 36 | private ExpressionStatement invokedUpon; 37 | private List> arguments; 38 | 39 | private MethodInvocation(ExpressionStatement invokedUpon, List> arguments) { 40 | this.invokedUpon = invokedUpon; 41 | this.arguments = arguments; 42 | } 43 | 44 | public static MethodInvocation create(Object invokedOn, List args) { 45 | List> arguments = new LinkedList<>(); 46 | for (Object arg : args) { 47 | if (arg instanceof CafeElement) { 48 | arguments.add((CafeElement) arg); 49 | } else { 50 | arguments.add(ExpressionStatement.of(arg)); 51 | } 52 | } 53 | return new MethodInvocation( 54 | ExpressionStatement.of(invokedOn), 55 | arguments 56 | ); 57 | } 58 | 59 | public List> getArguments() { 60 | return arguments; 61 | } 62 | 63 | public int getArity() { 64 | return arguments.size(); 65 | } 66 | 67 | public ExpressionStatement getInvokedUpon() { 68 | return invokedUpon; 69 | } 70 | 71 | // @Override 72 | // public List> children() { 73 | // return Collections.singletonList(invokedUpon); 74 | // } 75 | 76 | @Override 77 | protected MethodInvocation self() { 78 | return this; 79 | } 80 | 81 | @Override 82 | public void accept(CafeIrVisitor visitor) { 83 | visitor.visitMethodInvocation(this); 84 | } 85 | 86 | @Override 87 | public String toString() { 88 | return "MethodInvocation{" + 89 | "name=" + invokedUpon + 90 | "(...)}"; 91 | } 92 | } 93 | -------------------------------------------------------------------------------- /src/main/java/compiler/ir/NullStatement.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 NullStatement extends ExpressionStatement { 33 | @Override 34 | protected NullStatement self() { 35 | return this; 36 | } 37 | 38 | @Override 39 | public void accept(CafeIrVisitor visitor) { 40 | visitor.visitNull(this); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/main/java/compiler/ir/ObjectAccessStatement.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 ObjectAccessStatement extends ExpressionStatement { 36 | private ExpressionStatement accessedOn; 37 | private ExpressionStatement property; 38 | 39 | public ObjectAccessStatement(ExpressionStatement accessedOn, ExpressionStatement property) { 40 | this.accessedOn = accessedOn; 41 | this.property = property; 42 | } 43 | 44 | public static ObjectAccessStatement create(Object accessedOn, Object property) { 45 | return new ObjectAccessStatement( 46 | ExpressionStatement.of(accessedOn), 47 | ExpressionStatement.of(property) 48 | ); 49 | } 50 | 51 | public ExpressionStatement getProperty() { 52 | return property; 53 | } 54 | 55 | public ExpressionStatement getAccessedOn() { 56 | return accessedOn; 57 | } 58 | 59 | @Override 60 | public List> children() { 61 | LinkedList> list = new LinkedList<>(); 62 | list.add(accessedOn); 63 | list.add(property); 64 | return list; 65 | } 66 | 67 | @Override 68 | protected ObjectAccessStatement self() { 69 | return this; 70 | } 71 | 72 | @Override 73 | public void accept(CafeIrVisitor visitor) { 74 | visitor.visitObjectAccess(this); 75 | } 76 | 77 | @Override 78 | public String toString() { 79 | return "ObjectAccessStatement{" + 80 | "accessedOn=" + accessedOn + 81 | ", property=" + property + 82 | '}'; 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /src/main/java/compiler/ir/ObjectCreationStatement.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 | import java.util.Map; 35 | 36 | public class ObjectCreationStatement extends ExpressionStatement { 37 | private int index = -1; 38 | private Map> map; 39 | 40 | private ObjectCreationStatement(Map> map) { 41 | this.map = map; 42 | } 43 | 44 | public static ObjectCreationStatement of(Map> map) { 45 | return new ObjectCreationStatement(map); 46 | } 47 | 48 | public Map> getMap() { 49 | return map; 50 | } 51 | 52 | public void setIndex(int index) { 53 | this.index = index; 54 | } 55 | 56 | public int index() { 57 | return index; 58 | } 59 | 60 | @Override 61 | public List> children() { 62 | return new LinkedList<>(map.values()); 63 | } 64 | 65 | @Override 66 | protected ObjectCreationStatement self() { 67 | return this; 68 | } 69 | 70 | @Override 71 | public void accept(CafeIrVisitor visitor) { 72 | visitor.visitObjectCreation(this); 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /src/main/java/compiler/ir/OperatorType.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 | 34 | public enum OperatorType { 35 | 36 | PLUS("+"), 37 | MINUS("-"), 38 | TIMES("*"), 39 | DIVIDE("/"), 40 | MODULO("%"), 41 | 42 | POW("**"), 43 | FLOOR("//"), 44 | 45 | EQUALS("=="), 46 | NOTEQUALS("!="), 47 | LESS("<"), 48 | LESSOREQUALS("<="), 49 | MORE(">"), 50 | MOREOREQUALS(">="), 51 | 52 | BITOR("|"), 53 | BITAND("&"), 54 | BITXOR("^"), 55 | BITRIGHTSHIFT_SIGNED(">>"), 56 | BITRIGHTSHIFT_UNSIGNED(">>>"), 57 | BITLEFTSHIFT("<<"), 58 | 59 | AND("and"), 60 | OR("or"), 61 | NOT("not"), 62 | NOTOP("!"), 63 | 64 | IS("is"), 65 | ISNOT("isnot"), 66 | IN("in"), 67 | NOTIN("notin"); 68 | 69 | private final String symbol; 70 | 71 | private static final HashMap SYMBOL_MAPPING = new HashMap<>(); 72 | 73 | static { 74 | for (OperatorType op : values()) { 75 | SYMBOL_MAPPING.put(op.toString(), op); 76 | } 77 | } 78 | 79 | OperatorType(String symbol) { 80 | this.symbol = symbol; 81 | } 82 | 83 | @Override 84 | public String toString() { 85 | return symbol; 86 | } 87 | 88 | public static OperatorType of(Object value) { 89 | if (value instanceof OperatorType) { 90 | return (OperatorType) value; 91 | } 92 | if (SYMBOL_MAPPING.containsKey(value)) { 93 | return SYMBOL_MAPPING.get(value); 94 | } 95 | throw new IllegalArgumentException("An operator can't be create from " + value); 96 | } 97 | } 98 | -------------------------------------------------------------------------------- /src/main/java/compiler/ir/PropertyAccess.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 PropertyAccess extends ExpressionStatement { 33 | private String name; 34 | 35 | private PropertyAccess(String name) { 36 | this.name = name; 37 | } 38 | 39 | public static PropertyAccess of(Object name) { 40 | if (name instanceof String) 41 | return new PropertyAccess((String) name); 42 | throw cantConvert("String", name); 43 | } 44 | 45 | public String getName() { 46 | return name; 47 | } 48 | 49 | @Override 50 | protected PropertyAccess self() { 51 | return this; 52 | } 53 | 54 | @Override 55 | public void accept(CafeIrVisitor visitor) { 56 | visitor.visitPropertyAccess(this); 57 | } 58 | 59 | @Override 60 | public String toString() { 61 | return "PropertyAccess{" + 62 | "name='" + name + '\'' + 63 | '}'; 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /src/main/java/compiler/ir/ReferenceLookup.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 ReferenceLookup extends ExpressionStatement { 33 | 34 | private final String name; 35 | 36 | public ReferenceLookup(String name) { 37 | this.name = name; 38 | } 39 | 40 | public static ReferenceLookup of(Object name) { 41 | if (name instanceof ReferenceLookup) { 42 | return (ReferenceLookup) name; 43 | } 44 | if (name instanceof SymbolReference) { 45 | return new ReferenceLookup(((SymbolReference) name).getName()); 46 | } 47 | return new ReferenceLookup(name.toString()); 48 | } 49 | 50 | public SymbolReference resolveIn(ReferenceTable table) { 51 | return table.get(name); 52 | } 53 | 54 | public String getName() { 55 | return name; 56 | } 57 | 58 | @Override 59 | protected ReferenceLookup self() { 60 | return this; 61 | } 62 | 63 | @Override 64 | public void accept(CafeIrVisitor visitor) { 65 | visitor.visitReferenceLookup(this); 66 | } 67 | 68 | @Override 69 | public String toString() { 70 | return "ReferenceLookup{" + 71 | "name='" + name + '\'' + 72 | '}'; 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /src/main/java/compiler/ir/ReferenceTable.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.LinkedHashMap; 34 | import java.util.Map; 35 | import java.util.Set; 36 | 37 | public class ReferenceTable { 38 | private ReferenceTable parent; 39 | private final Map table = new LinkedHashMap<>(); 40 | 41 | public ReferenceTable() { 42 | this(null); 43 | } 44 | 45 | public ReferenceTable(ReferenceTable parent) { 46 | this.parent = parent; 47 | } 48 | 49 | public ReferenceTable parent() { 50 | return this.parent; 51 | } 52 | 53 | public ReferenceTable add(SymbolReference reference) { 54 | table.put(reference.getName(), reference); 55 | return this; 56 | } 57 | 58 | public ReferenceTable addToTopLevel(SymbolReference reference) { 59 | if (parent != null) 60 | parent.addToTopLevel(reference); 61 | else 62 | add(reference); 63 | return this; 64 | } 65 | 66 | public SymbolReference get(String name) { 67 | SymbolReference reference = table.get(name); 68 | if (reference != null) 69 | return reference; 70 | if (parent != null) 71 | return parent.get(name); 72 | return null; 73 | } 74 | 75 | public Set getOwnedReferences() { 76 | return Collections.unmodifiableSet(table.keySet()); 77 | } 78 | 79 | public void relink(ReferenceTable parent) { 80 | this.parent = parent; 81 | } 82 | 83 | public ReferenceTable fork() { 84 | return new ReferenceTable(this); 85 | } 86 | 87 | public boolean hasReferenceFor(String name) { 88 | return table.containsKey(name) || parent != null && parent.hasReferenceFor(name); 89 | } 90 | } 91 | -------------------------------------------------------------------------------- /src/main/java/compiler/ir/ReturnStatement.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 ReturnStatement extends CafeStatement { 36 | private CafeStatement expressionStatement; 37 | private boolean isReturningVoid = false; 38 | 39 | private ReturnStatement(ExpressionStatement expression) { 40 | setExpressionStatement(expression); 41 | } 42 | 43 | public static ReturnStatement of(Object value) { 44 | return new ReturnStatement(ExpressionStatement.of(value)); 45 | } 46 | 47 | private void setExpressionStatement(CafeStatement stat) { 48 | if (stat != null) { 49 | this.expressionStatement = stat; 50 | } else { 51 | this.expressionStatement = null; 52 | } 53 | } 54 | 55 | public CafeStatement getExpressionStatement() { 56 | return expressionStatement; 57 | } 58 | 59 | 60 | @Override 61 | public List> children() { 62 | if (expressionStatement != null) 63 | return Collections.singletonList(expressionStatement); 64 | return Collections.emptyList(); 65 | } 66 | 67 | @Override 68 | protected ReturnStatement self() { 69 | return this; 70 | } 71 | 72 | @Override 73 | public void accept(CafeIrVisitor visitor) { 74 | visitor.visitReturn(this); 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /src/main/java/compiler/ir/SliceExpression.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 SliceExpression extends ExpressionStatement { 33 | 34 | private ExpressionStatement slicedOn; 35 | private ExpressionStatement beginIndex; 36 | private ExpressionStatement endIndex; 37 | 38 | private SliceExpression(ExpressionStatement slicedOn) { 39 | this.slicedOn = slicedOn; 40 | } 41 | 42 | public static SliceExpression slice(Object slicedOn) { 43 | return new SliceExpression(ExpressionStatement.of(slicedOn)); 44 | } 45 | 46 | public SliceExpression beginsAt(Object beginIndex) { 47 | this.beginIndex = ExpressionStatement.of(beginIndex); 48 | return this; 49 | } 50 | 51 | public SliceExpression endsAt(Object endIndex) { 52 | this.endIndex = ExpressionStatement.of(endIndex); 53 | return this; 54 | } 55 | 56 | public SliceExpression range(Object beginIndex, Object endIndex) { 57 | this.beginIndex = ExpressionStatement.of(beginIndex); 58 | this.endIndex = ExpressionStatement.of(endIndex); 59 | return this; 60 | } 61 | 62 | public ExpressionStatement getBeginIndex() { 63 | return beginIndex; 64 | } 65 | 66 | public ExpressionStatement getEndIndex() { 67 | return endIndex; 68 | } 69 | 70 | public ExpressionStatement getSlicedOn() { 71 | return slicedOn; 72 | } 73 | 74 | @Override 75 | protected SliceExpression self() { 76 | return this; 77 | } 78 | 79 | @Override 80 | public void accept(CafeIrVisitor visitor) { 81 | visitor.visitSlice(this); 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /src/main/java/compiler/ir/SubscriptExpression.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 SubscriptExpression extends ExpressionStatement { 33 | private ExpressionStatement subscriptOf; 34 | private ExpressionStatement subscriptIndex; 35 | 36 | public SubscriptExpression(ExpressionStatement subscriptOf, ExpressionStatement subscriptIndex) { 37 | this.subscriptOf = subscriptOf; 38 | this.subscriptIndex = subscriptIndex; 39 | } 40 | 41 | public static SubscriptExpression create(Object subscriptOf, Object index) { 42 | return new SubscriptExpression( 43 | ExpressionStatement.of(subscriptOf), 44 | ExpressionStatement.of(index) 45 | ); 46 | } 47 | 48 | public ExpressionStatement getSubscriptOf() { 49 | return subscriptOf; 50 | } 51 | 52 | public ExpressionStatement getSubscriptIndex() { 53 | return subscriptIndex; 54 | } 55 | 56 | @Override 57 | protected SubscriptExpression self() { 58 | return this; 59 | } 60 | 61 | @Override 62 | public void accept(CafeIrVisitor visitor) { 63 | visitor.visitSubscript(this); 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /src/main/java/compiler/ir/SymbolReference.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 SymbolReference extends CafeElement { 33 | 34 | @Override 35 | protected SymbolReference self() { 36 | return this; 37 | } 38 | 39 | @Override 40 | public void accept(CafeIrVisitor visitor) { 41 | visitor.visitSymbolReference(this); 42 | } 43 | 44 | public enum Kind { 45 | VAR, CONST 46 | } 47 | 48 | public enum Scope { 49 | GLOBAL, LOCAL 50 | } 51 | 52 | private final String name; 53 | private final Kind kind; 54 | private final Scope scope; 55 | private int index = -1; 56 | 57 | private SymbolReference(String name, Kind kind, Scope scope) { 58 | this.kind = kind; 59 | this.name = name; 60 | this.scope = scope; 61 | } 62 | 63 | public static SymbolReference of(String name, Kind kind, Scope scope) { 64 | return new SymbolReference(name, kind, scope); 65 | } 66 | 67 | public String getName() { 68 | return name; 69 | } 70 | 71 | public Kind getKind() { 72 | return kind; 73 | } 74 | 75 | public boolean isGlobal() { 76 | return scope == Scope.GLOBAL; 77 | } 78 | 79 | public boolean isLocal() { 80 | return scope == Scope.LOCAL; 81 | } 82 | 83 | public void setIndex(int index) { 84 | this.index = index; 85 | } 86 | 87 | public int getIndex() { 88 | return index; 89 | } 90 | 91 | @Override 92 | public String toString() { 93 | return "SymbolReference{" + 94 | "name='" + name + '\'' + 95 | ", kind=" + kind + 96 | ", scope=" + scope + 97 | ", index=" + index + 98 | '}'; 99 | } 100 | } 101 | -------------------------------------------------------------------------------- /src/main/java/compiler/ir/TargetFuncWrapper.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 TargetFuncWrapper { 33 | CafeFunction getTarget(); 34 | } 35 | -------------------------------------------------------------------------------- /src/main/java/compiler/ir/ThisStatement.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 ThisStatement extends ExpressionStatement { 33 | 34 | private final boolean isGlobal; 35 | 36 | private ThisStatement(boolean isGlobal) { 37 | this.isGlobal = isGlobal; 38 | } 39 | 40 | public static ThisStatement create(boolean isGlobal) { 41 | return new ThisStatement(isGlobal); 42 | } 43 | 44 | public boolean isGlobal() { 45 | return isGlobal; 46 | } 47 | 48 | @Override 49 | protected ThisStatement self() { 50 | return this; 51 | } 52 | 53 | @Override 54 | public void accept(CafeIrVisitor visitor) { 55 | visitor.visitThis(this); 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /src/main/java/compiler/ir/UnaryExpression.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 UnaryExpression extends ExpressionStatement { 36 | private final OperatorType type; 37 | private ExpressionStatement expressionStatement; 38 | 39 | private UnaryExpression(OperatorType type, ExpressionStatement expressionStatement) { 40 | this.type = type; 41 | this.expressionStatement = expressionStatement; 42 | } 43 | 44 | public static UnaryExpression create(Object type, Object expr) { 45 | return new UnaryExpression( 46 | OperatorType.of(type), 47 | ExpressionStatement.of(expr) 48 | ); 49 | } 50 | 51 | public OperatorType getType() { 52 | return type; 53 | } 54 | 55 | @Override 56 | public List> children() { 57 | return Collections.singletonList(expressionStatement); 58 | } 59 | 60 | @Override 61 | protected UnaryExpression self() { 62 | return this; 63 | } 64 | 65 | @Override 66 | public String toString() { 67 | return "UnaryExpression{" + 68 | type + 69 | expressionStatement + 70 | '}'; 71 | } 72 | 73 | @Override 74 | public void accept(CafeIrVisitor visitor) { 75 | visitor.visitUnaryExpression(this); 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /src/main/java/compiler/main/SourceFileManager.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.main; 31 | 32 | import compiler.util.Context; 33 | import compiler.util.Log; 34 | 35 | import java.io.*; 36 | import java.util.ArrayList; 37 | import java.util.List; 38 | 39 | import static compiler.util.Log.Type.INVALID_CLI_FILE_PATH; 40 | import static compiler.util.Messages.message; 41 | 42 | /** 43 | * A temporary class to register source file 44 | * Status: incomplete 45 | * 46 | * @author Dhyey 47 | */ 48 | public class SourceFileManager { 49 | public static final Context.Key fileKey = new Context.Key<>(); 50 | private File file; 51 | private Log log; 52 | 53 | public static SourceFileManager instance(Context context) { 54 | SourceFileManager instance = context.get(fileKey); 55 | if (instance == null) { 56 | instance = new SourceFileManager(context); 57 | } 58 | 59 | return instance; 60 | } 61 | 62 | private SourceFileManager(Context context) { 63 | context.put(fileKey, this); 64 | log = Log.instance(context); 65 | } 66 | 67 | public void setSourceFile(String path) { 68 | File file = new File(path); 69 | if (file.exists() && !file.isDirectory()) { 70 | this.file = file; 71 | } else { 72 | log.report(INVALID_CLI_FILE_PATH, null, 73 | message(INVALID_CLI_FILE_PATH, path)); 74 | } 75 | } 76 | 77 | public void setSourceFile(File file) { 78 | this.file = file; 79 | } 80 | 81 | public List asCharList() { 82 | char ch; 83 | List list = new ArrayList<>(100); 84 | BufferedReader br; 85 | 86 | try { 87 | br = new BufferedReader(new FileReader(file)); 88 | 89 | int c = br.read(); 90 | while (c != -1) { 91 | ch = (char) c; 92 | list.add(ch); 93 | c = br.read(); 94 | } 95 | br.close(); 96 | } catch (FileNotFoundException e) { 97 | e.printStackTrace(); 98 | } catch (IOException e) { 99 | e.printStackTrace(); 100 | } 101 | 102 | return list; 103 | } 104 | } 105 | -------------------------------------------------------------------------------- /src/main/java/compiler/main/cli/Command.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.main.cli; 31 | 32 | import compiler.main.Main; 33 | 34 | import java.util.HashMap; 35 | import java.util.Map; 36 | 37 | public interface Command { 38 | Map commands = new HashMap<>(); 39 | 40 | enum CommandName { 41 | COMPILE("CompileCommand"), 42 | RUN("RunCommand"); 43 | 44 | CommandName(String className) { 45 | this.commandClassName = className; 46 | } 47 | 48 | public String getCommandClassName() { 49 | return commandClassName; 50 | } 51 | 52 | String commandClassName; 53 | } 54 | 55 | Main.Result execute(); 56 | 57 | static void registerCommand(CommandName type, Command command) { 58 | commands.put(type, command); 59 | } 60 | 61 | static Command getCommand(CommandName type) { 62 | return commands.get(type); 63 | } 64 | 65 | static void initCommands() { 66 | for (CommandName command : CommandName.values()) { 67 | try { 68 | Class.forName("compiler.main.cli." + command.getCommandClassName()); 69 | } catch (ClassNotFoundException e) { 70 | e.printStackTrace(); 71 | } 72 | } 73 | } 74 | 75 | default void callRun() { 76 | 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /src/main/java/compiler/main/cli/CompileCommand.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.main.cli; 31 | 32 | import com.beust.jcommander.Parameter; 33 | import com.beust.jcommander.Parameters; 34 | import compiler.main.CafeCompiler; 35 | import compiler.main.CompilerResult; 36 | import compiler.main.Main; 37 | 38 | @Parameters(commandNames = {"-c"}, commandDescription = "Compiles Cafe source files") 39 | public class CompileCommand implements Command { 40 | 41 | @Parameter(description = "[source-file].cafe", required = true) 42 | String source; 43 | 44 | private CompileCommand() { 45 | } 46 | 47 | static { 48 | Command.registerCommand(CommandName.COMPILE, new CompileCommand()); 49 | } 50 | 51 | @Override 52 | public Main.Result execute() { 53 | CompilerResult result = new CafeCompiler(source).compile(); 54 | 55 | if (result.isOk()) { 56 | result.writeByteCode(); 57 | } 58 | 59 | return result.getResult(); 60 | } 61 | 62 | 63 | } 64 | -------------------------------------------------------------------------------- /src/main/java/compiler/main/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.main; -------------------------------------------------------------------------------- /src/main/java/compiler/parser/CharReader.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.parser; 31 | 32 | import java.util.ArrayList; 33 | import java.util.List; 34 | import java.util.stream.Collectors; 35 | 36 | public class CharReader { 37 | 38 | /** 39 | * Input Buffer 40 | */ 41 | protected List buff; 42 | 43 | protected int lineNumber = 1; 44 | protected int column = 0; 45 | 46 | /** 47 | * Input Buffer pointer 48 | */ 49 | protected int bp = -1; 50 | 51 | /** 52 | * Input Buffer Length 53 | */ 54 | private int buffLen; 55 | 56 | /** 57 | * Saved buffer 58 | */ 59 | protected List sbuff; 60 | 61 | /** 62 | * Saved Buffer pointer 63 | */ 64 | private int sp = -1; 65 | 66 | /** 67 | * Saved Buffer max capacity 68 | */ 69 | private int sbuffCapacity = 128; 70 | 71 | /** 72 | * Current character 73 | */ 74 | protected char ch; 75 | // 76 | // protected CharReader(CharSequence input) { 77 | // this(input.toString().toCharArray()); 78 | // } 79 | 80 | protected CharReader(List input) { 81 | buff = input; 82 | buffLen = buff.size(); 83 | scanChar(); 84 | 85 | sbuff = new ArrayList<>(sbuffCapacity); 86 | } 87 | 88 | protected void scanChar() { 89 | if (bp < buffLen - 1) { 90 | ch = buff.get(++bp); 91 | if (ch == '\n') { 92 | lineNumber++; 93 | column = 0; 94 | } else if (ch == '\t') 95 | column += 4; 96 | else 97 | column++; 98 | } else 99 | ch = Character.MIN_VALUE; 100 | } 101 | 102 | protected void putChar(char c) { 103 | sbuff.add(c); 104 | } 105 | 106 | /** 107 | * @param clearSavedBuffer - if true, clears sbuff 108 | * @return saved buffer char array 109 | */ 110 | protected List getSavedBuffer(boolean clearSavedBuffer) { 111 | if (clearSavedBuffer) { 112 | clearSavedBufer(); 113 | } 114 | return sbuff; 115 | } 116 | 117 | protected String getSavedBufferAsString(boolean clearSavedBuffer) { 118 | String s = sbuff.stream() 119 | .map(e -> e.toString()) 120 | .collect(Collectors.joining()); 121 | if (clearSavedBuffer) { 122 | clearSavedBufer(); 123 | } 124 | return s; 125 | } 126 | 127 | private void clearSavedBufer() { 128 | sbuff = new ArrayList<>(sbuffCapacity); 129 | } 130 | 131 | } 132 | -------------------------------------------------------------------------------- /src/main/java/compiler/parser/Lexer.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.parser; 31 | 32 | import compiler.parser.Tokens.Token; 33 | 34 | public interface Lexer { 35 | 36 | void nextToken(); 37 | 38 | Token token(); 39 | 40 | Token prevToken(); 41 | 42 | } 43 | -------------------------------------------------------------------------------- /src/main/java/compiler/parser/Parser.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.parser; 31 | 32 | import compiler.ast.Node; 33 | 34 | public abstract class Parser { 35 | abstract Parser instance(ParserFactory factory, Lexer lexer); 36 | 37 | public abstract Node parse(); 38 | } -------------------------------------------------------------------------------- /src/main/java/compiler/parser/ParserFactory.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.parser; 31 | 32 | import compiler.util.Context; 33 | import compiler.util.Log; 34 | 35 | import java.util.HashMap; 36 | import java.util.List; 37 | import java.util.Map; 38 | 39 | /** 40 | * A factory to generate parsers. To register a new concrete parser the 41 | * following steps have to be followed: 42 | *
    43 | *
  1. Create a concrete class that implements Parser interface. 44 | *
  2. Add the parser name in the ParserType with the class name of 45 | * the above created new parser as its argument. 46 | *
  3. In the newly created parser class, inside static block, call the method 47 | * ParserFactory.registerParser(...), to register the newly created 48 | * parser. 49 | *
50 | * 51 | * @author Dhyey 52 | */ 53 | public class ParserFactory { 54 | protected final static Context.Key parserFactoryKey = new Context.Key<>(); 55 | 56 | private final static Map parsers = new HashMap<>(); 57 | 58 | final Log log; 59 | final ScannerFactory scannerFactory; 60 | 61 | public static ParserFactory instance(Context context) { 62 | ParserFactory instance = context.get(parserFactoryKey); 63 | 64 | if (instance == null) { 65 | instance = new ParserFactory(context); 66 | } 67 | 68 | return instance; 69 | } 70 | 71 | public static void registerParser(ParserType type, Parser parser) { 72 | Object o = parsers.put(type, parser); 73 | if (o != null) { 74 | throw new AssertionError("Parser of type " + type + " is already Registered"); 75 | } 76 | } 77 | 78 | private ParserFactory(Context context) { 79 | context.put(parserFactoryKey, this); 80 | ParserType.init(); 81 | 82 | this.log = Log.instance(context); 83 | this.scannerFactory = ScannerFactory.instance(context); 84 | } 85 | 86 | public Parser newParser(ParserType type, List input) { 87 | Parser p = parsers.get(type); 88 | if (p == null) { 89 | throw new AssertionError("Parser of type " + type + " is not registered"); 90 | } 91 | 92 | Lexer lexer = scannerFactory.newScanner(input); 93 | 94 | return p.instance(this, lexer); 95 | } 96 | } 97 | -------------------------------------------------------------------------------- /src/main/java/compiler/parser/ParserType.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.parser; 31 | 32 | /** 33 | * A registry of the concrete implementation of parsers. 34 | * 35 | * @author Dhyey 36 | */ 37 | public enum ParserType { 38 | MAINPARSER("MainParser"); 39 | 40 | String parserClassName; 41 | 42 | ParserType(String className) { 43 | this.parserClassName = className; 44 | } 45 | 46 | String getParserClassName() { 47 | return parserClassName; 48 | } 49 | 50 | static void init() { 51 | for (ParserType type : ParserType.values()) { 52 | try { 53 | Class.forName("compiler.parser." + type.getParserClassName()); 54 | } catch (ClassNotFoundException e) { 55 | e.printStackTrace(); 56 | } 57 | } 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /src/main/java/compiler/parser/Scanner.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.parser; 31 | 32 | import compiler.parser.Tokens.Token; 33 | 34 | import java.util.List; 35 | 36 | public class Scanner implements Lexer { 37 | 38 | static { 39 | ScannerFactory.registerScanner(new Scanner()); 40 | } 41 | 42 | private Tokens tokens; 43 | private Token token; 44 | private Token prevToken; 45 | 46 | private Tokenizer tokenizer; 47 | 48 | private Scanner() { 49 | } 50 | 51 | private Scanner(ScannerFactory scannerFactory, List buff) { 52 | this.tokens = scannerFactory.tokens; 53 | token = prevToken = null; 54 | 55 | tokenizer = new Tokenizer(scannerFactory, buff); 56 | } 57 | 58 | protected Scanner instance(ScannerFactory scannerFactory, List input) { 59 | return new Scanner(scannerFactory, input); 60 | } 61 | 62 | @Override 63 | public void nextToken() { 64 | prevToken = token; 65 | token = tokenizer.readToken(); 66 | } 67 | 68 | @Override 69 | public Token token() { 70 | return token; 71 | } 72 | 73 | @Override 74 | public Token prevToken() { 75 | return prevToken; 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /src/main/java/compiler/parser/ScannerFactory.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.parser; 31 | 32 | import compiler.util.Context; 33 | import compiler.util.Log; 34 | 35 | import java.util.List; 36 | 37 | public class ScannerFactory { 38 | public static final Context.Key scannerFactoryKey = new Context.Key<>(); 39 | 40 | final Log log; 41 | final Tokens tokens; 42 | 43 | private static Scanner scanner; 44 | 45 | static { 46 | try { 47 | Class.forName("compiler.parser.Scanner"); 48 | } catch (ClassNotFoundException e) { 49 | e.printStackTrace(); 50 | } 51 | } 52 | 53 | public static ScannerFactory instance(Context context) { 54 | ScannerFactory instance = context.get(scannerFactoryKey); 55 | if (instance == null) 56 | instance = new ScannerFactory(context); 57 | return instance; 58 | } 59 | 60 | private ScannerFactory(Context context) { 61 | context.put(scannerFactoryKey, this); 62 | log = Log.instance(context); 63 | tokens = Tokens.instance(context); 64 | } 65 | 66 | static void registerScanner(Scanner sc) { 67 | scanner = sc; 68 | } 69 | 70 | public Lexer newScanner(List input) { 71 | return scanner.instance(this, input); 72 | } 73 | 74 | } 75 | -------------------------------------------------------------------------------- /src/main/java/compiler/parser/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.parser; -------------------------------------------------------------------------------- /src/main/java/compiler/util/Context.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.util; 31 | 32 | import java.util.HashMap; 33 | import java.util.Map; 34 | 35 | /** 36 | * Context ensures that single context is used for each compiler phase invocation. 37 | *

Every phase registers itself with this context

38 | * 39 | * @author Dhyey 40 | */ 41 | public class Context { 42 | public static class Key { 43 | } 44 | 45 | protected final Map, Object> map = new HashMap<>(); 46 | 47 | public void put(Key key, T fac) { 48 | Object old = map.put(key, fac); 49 | if (old != null) { 50 | throw new AssertionError("duplicate value"); 51 | } 52 | } 53 | 54 | @SuppressWarnings("unchecked") 55 | public T get(Key key) { 56 | Object o = map.get(key); 57 | return (T) o; 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /src/main/java/compiler/util/Log.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.util; 31 | 32 | import java.util.LinkedList; 33 | import java.util.List; 34 | 35 | public class Log { 36 | public static final Context.Key logKey = new Context.Key<>(); 37 | 38 | private final List issues; 39 | 40 | protected Log(Context context) { 41 | context.put(logKey, this); 42 | 43 | issues = new LinkedList<>(); 44 | } 45 | 46 | public static Log instance(Context context) { 47 | Log instance = context.get(logKey); 48 | if (instance == null) 49 | instance = new Log(context); 50 | return instance; 51 | } 52 | 53 | public int entries() { 54 | return issues.size(); 55 | } 56 | 57 | public static final class Issue { 58 | private final Position position; 59 | private final Type type; 60 | private final String description; 61 | 62 | private Issue(Type type, Position position, String description) { 63 | this.type = type; 64 | this.description = description; 65 | this.position = position; 66 | } 67 | 68 | public String getDescription() { 69 | return description; 70 | } 71 | 72 | public Position getPosition() { 73 | return position; 74 | } 75 | 76 | public Type getType() { 77 | return type; 78 | } 79 | } 80 | 81 | public enum Type { 82 | ERROR, 83 | WARNING, 84 | SUCCESS, 85 | SOURCE_POSITION, 86 | 87 | // CLI errors 88 | NO_FILE_PATH_GIVEN_IN_CLI, 89 | INVALID_CLI_FILE_PATH, 90 | MODULE_NOT_FOUND, 91 | 92 | // Lex error 93 | ILLEGAL_CHARACTER, 94 | INVALID_IDENTIFIER, 95 | INVALID_FRACTIONAL_VAL, 96 | EOF, 97 | EOF_PARSING_COMMENT, 98 | 99 | // Parsing errors 100 | SYMBOL_EXPECTED, 101 | INVALID_EXPRESSION, 102 | INVALID_IMPORT_FILE, 103 | 104 | // Semantic errors 105 | SYMBOL_NOT_DECLARED, 106 | LHS_EXPR_ERROR, 107 | DUPLICATE_SYMBOL, 108 | REASSIGN_CONSTANT, 109 | RETURN_OUTSIDE_BLOCK; 110 | } 111 | 112 | public void report(Type err, Position pos, String description) { 113 | issues.add( 114 | new Issue(err, pos, description) 115 | ); 116 | } 117 | 118 | public void printIssues() { 119 | for (Issue issue : issues) { 120 | Messages.error(issue.getDescription()); 121 | } 122 | } 123 | } 124 | -------------------------------------------------------------------------------- /src/main/java/compiler/util/Position.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.util; 31 | 32 | public class Position { 33 | private final int startLine; 34 | private final int startColumn; 35 | private final int endLine; 36 | private final int endColumn; 37 | 38 | private Position(int startColumn, int startLine, int endColumn, int endLine) { 39 | this.startColumn = startColumn; 40 | this.startLine = startLine; 41 | this.endColumn = endColumn; 42 | this.endLine = endLine; 43 | } 44 | 45 | public static Position of(int column, int line) { 46 | return new Position(column, line, column, line); 47 | } 48 | 49 | public static Position of(int startColumn, int startLine, int endColumn, int endLine) { 50 | return new Position(startColumn, startLine, endColumn, endLine); 51 | } 52 | 53 | public int getStartColumn() { 54 | return startColumn; 55 | } 56 | 57 | public int getStartLine() { 58 | return startLine; 59 | } 60 | 61 | public int getEndColumn() { 62 | return endColumn; 63 | } 64 | 65 | public int getEndLine() { 66 | return endLine; 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /src/main/java/compiler/util/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.util; -------------------------------------------------------------------------------- /src/main/java/library/DFunc.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 library; 31 | 32 | import java.lang.invoke.MethodHandle; 33 | import java.util.ArrayList; 34 | import java.util.List; 35 | 36 | /** 37 | * Represents a Cafe Function. Every function is an instance of this class. Holds a reference to a JVM method using a method-handle. 38 | */ 39 | public class DFunc extends DObject { 40 | private MethodHandle handle; 41 | private boolean isVarity = false; 42 | 43 | public DFunc(DObject __proto__, MethodHandle handle) { 44 | super(__proto__); 45 | this.handle = handle; 46 | if (handle.isVarargsCollector()) 47 | isVarity = true; 48 | } 49 | 50 | public Object invoke(Object... args) throws Throwable { 51 | List arguments = new ArrayList<>(); 52 | int pos = 0; 53 | if (handle.type() 54 | .parameterArray()[0] != DObject.class) { 55 | // if 1st arg is already bound, omit the thisP arg from args. 56 | pos = 1; 57 | } 58 | for (int i = pos; i < args.length; i++) 59 | arguments.add(args[i]); 60 | 61 | return handle.invokeWithArguments(arguments); 62 | } 63 | 64 | public MethodHandle handle() { 65 | return handle; 66 | } 67 | 68 | public boolean isVarity() { 69 | return isVarity; 70 | } 71 | 72 | @Override 73 | public String toString() { 74 | return "Function{ }"; 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /src/main/java/library/DList.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 library; 31 | 32 | import runtime.DObjectCreator; 33 | 34 | import java.util.ArrayList; 35 | 36 | public class DList extends DObject implements Subscriptable, Slicable { 37 | private ArrayList list; 38 | 39 | public DList(DObject __proto__) { 40 | super(__proto__); 41 | list = new ArrayList<>(); 42 | } 43 | 44 | public DList(DObject __proto__, ArrayList list) { 45 | super(__proto__); 46 | this.list = list; 47 | } 48 | 49 | public void add(Object object) { 50 | list.add(object); 51 | } 52 | 53 | public void remove(Object object) { 54 | list.remove(object); 55 | } 56 | 57 | public void removeAt(int index) { 58 | list.remove(index); 59 | } 60 | 61 | public Object get(int index) { 62 | return list.get(index); 63 | } 64 | 65 | public int size() { 66 | return list.size(); 67 | } 68 | 69 | @Override 70 | public Object getSubscript(Object key) { 71 | return get((Integer) key); 72 | } 73 | 74 | @Override 75 | public void setSubscript(Object key, Object value) { 76 | list.set((Integer) key, value); 77 | } 78 | 79 | @Override 80 | public DList slice(int s, int e) { 81 | return DObjectCreator.createList(new ArrayList<>(list.subList(s, e))); 82 | } 83 | 84 | @Override 85 | public void setSlice(int s, int e, Object value) { 86 | DList o = DObjectCreator.createList(); 87 | if (value instanceof DList) 88 | o = (DList) value; 89 | else 90 | o.add(value); 91 | int i, j, size = o.size(); 92 | for (i = s, j = 0; i < e && j < size; i++, j++) { 93 | list.set(i, o.get(j)); 94 | } 95 | while (j < o.size()) { 96 | list.add(o.get(j++)); 97 | } 98 | 99 | while (i < e) { 100 | list.remove(i); 101 | e--; 102 | } 103 | } 104 | 105 | @Override 106 | public String toString() { 107 | return list.toString(); 108 | } 109 | } 110 | -------------------------------------------------------------------------------- /src/main/java/library/Slicable.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 library; 31 | 32 | public interface Slicable { 33 | DList slice(int s, int e); 34 | 35 | void setSlice(int s, int e, Object value); 36 | } 37 | -------------------------------------------------------------------------------- /src/main/java/library/Subscriptable.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 library; 31 | 32 | public interface Subscriptable { 33 | Object getSubscript(Object key); 34 | 35 | void setSubscript(Object key, Object value); 36 | } 37 | -------------------------------------------------------------------------------- /src/main/java/library/base/CFunc.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 library.base; 31 | 32 | public class CFunc { 33 | } 34 | -------------------------------------------------------------------------------- /src/main/java/library/base/CFuncProto.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 library.base; 31 | 32 | import library.DFunc; 33 | import library.DObject; 34 | import runtime.DObjectCreator; 35 | 36 | import java.lang.invoke.MethodHandle; 37 | import java.lang.invoke.MethodHandles; 38 | 39 | public class CFuncProto { 40 | public static DFunc bind(DObject func, DObject thisP, Object... args) { 41 | MethodHandle h1 = ((DFunc) func).handle(); 42 | MethodHandle h2 = null; 43 | boolean isVarity = ((DFunc) func).isVarity(); 44 | 45 | if (thisP == null && args.length > 0) 46 | // this is required as arguments array is not spread when thisP is null. 47 | // args = { args[] } 48 | args = (Object[]) args[0]; 49 | 50 | if (thisP != null) { 51 | h2 = h1.bindTo(thisP); 52 | } 53 | 54 | if (args.length > 0) { 55 | int pos = 1; 56 | MethodHandle mh = h1; 57 | if (thisP != null) { 58 | pos = 0; 59 | mh = h2; 60 | } 61 | h2 = MethodHandles.insertArguments(mh, pos, args); 62 | } 63 | 64 | if (h2 != null) { 65 | if (isVarity) 66 | h2 = h2.asVarargsCollector(Object[].class); 67 | return DObjectCreator.createFunc(h2); 68 | } 69 | 70 | return (DFunc) func; 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /src/main/java/library/base/CList.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 library.base; 31 | 32 | public class CList { 33 | } 34 | -------------------------------------------------------------------------------- /src/main/java/library/base/CListProto.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 library.base; 31 | 32 | import library.DList; 33 | import library.DObject; 34 | 35 | public class CListProto { 36 | public static void add(DObject object, Object value) { 37 | ((DList) object).add(value); 38 | } 39 | 40 | public static Object get(DObject object, Object index) { 41 | return ((DList) object).get((Integer) index); 42 | } 43 | 44 | public static void remove(DObject object, Object value) { 45 | ((DList) object).remove(value); 46 | } 47 | 48 | public static void removeAt(DObject object, Object value) { 49 | ((DList) object).removeAt((Integer) value); 50 | } 51 | 52 | public static int size(DObject object) { 53 | return ((DList) object).size(); 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /src/main/java/library/base/CObject.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 library.base; 31 | 32 | import library.DObject; 33 | 34 | public class CObject { 35 | public static DObject create(DObject e, DObject o) { 36 | return new DObject(o); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/main/java/library/base/CObjectProto.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 library.base; 31 | 32 | public class CObjectProto { 33 | 34 | } 35 | -------------------------------------------------------------------------------- /src/main/java/library/io/BasicIO.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 library.io; 31 | 32 | import library.DObject; 33 | 34 | public class BasicIO { 35 | public static void print(DObject b, Object o) { 36 | System.out.print(o); 37 | } 38 | 39 | public static void println(DObject b, Object o) { 40 | System.out.println(o); 41 | } 42 | 43 | public static String input(DObject b) { 44 | return System.console() 45 | .readLine(); 46 | } 47 | 48 | public static String input(DObject b, String s) { 49 | println(b, s); 50 | return input(b); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /src/main/java/runtime/CafeURLClassLoader.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 runtime; 31 | 32 | import runtime.imports.CafeModulePath; 33 | 34 | import java.io.File; 35 | import java.net.MalformedURLException; 36 | import java.net.URL; 37 | import java.net.URLClassLoader; 38 | 39 | public class CafeURLClassLoader extends URLClassLoader { 40 | 41 | private CafeURLClassLoader(URL[] urls) { 42 | super(urls); 43 | } 44 | 45 | public static CafeURLClassLoader with(CafeModulePath module) { 46 | try { 47 | URL url = cafePathtoURL(module); 48 | URL[] urls = {url}; 49 | return new CafeURLClassLoader(urls); 50 | } catch (MalformedURLException e) { 51 | e.printStackTrace(); 52 | return null; 53 | } 54 | } 55 | 56 | private static URL cafePathtoURL(CafeModulePath path) throws MalformedURLException { 57 | String importPath = path.asString(); 58 | String classpath = importPath + ".class"; 59 | File f = new File(classpath); 60 | classpath = f.getParentFile() 61 | .getPath(); 62 | URL url = new File(classpath).toURI() 63 | .toURL(); 64 | return url; 65 | } 66 | 67 | public void addModule(CafeModulePath path) { 68 | try { 69 | super.addURL(cafePathtoURL(path)); 70 | } catch (MalformedURLException e) { 71 | e.printStackTrace(); 72 | } 73 | } 74 | 75 | public Class loadModule(CafeModulePath module) throws ClassNotFoundException { 76 | return super.loadClass(module.getClassName()); 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /src/main/java/runtime/ExportMap.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 runtime; 31 | 32 | import library.DObject; 33 | 34 | import java.util.Map; 35 | 36 | public final class ExportMap { 37 | private Class clazz; 38 | private DObject object; 39 | Map exports; 40 | 41 | private ExportMap(Class clazz) { 42 | this.clazz = clazz; 43 | } 44 | 45 | public static ExportMap of(Class clazz) { 46 | return new ExportMap(clazz); 47 | } 48 | 49 | public ExportMap with(Map exports) { 50 | this.exports = exports; 51 | return this; 52 | } 53 | 54 | public Object getExport(String name) { 55 | return exports.get(name); 56 | } 57 | 58 | public DObject getAsDObject() { 59 | if (object != null) 60 | return object; 61 | object = DObjectCreator.create(); 62 | for (Map.Entry entry : exports.entrySet()) { 63 | object.define(entry.getKey(), entry.getValue()); 64 | } 65 | return object; 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /src/main/java/runtime/JavaImports.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 runtime; 31 | 32 | import library.DObject; 33 | import library.base.CFunc; 34 | import library.base.CList; 35 | import library.base.CObject; 36 | import library.io.BasicIO; 37 | import runtime.imports.JavaModulePath; 38 | 39 | import java.util.HashMap; 40 | import java.util.Map; 41 | 42 | import static runtime.DObjectCreator.generateFrom; 43 | 44 | public final class JavaImports { 45 | private static Map map = new HashMap<>(); 46 | private static final Map DEFAULT_IMPORTS; 47 | private static final Map DEFAULT_MODULE_PATHS; 48 | 49 | static { 50 | DEFAULT_IMPORTS = new HashMap() {{ 51 | put("Object", generateFrom(CObject.class)); 52 | put("Function", generateFrom(CFunc.class)); 53 | put("List", generateFrom(CList.class)); 54 | put("cmd", generateFrom(BasicIO.class)); 55 | }}; 56 | 57 | DEFAULT_MODULE_PATHS = new HashMap() {{ 58 | put(new JavaModulePath("library.base.CObject", CObject.class), "Object"); 59 | put(new JavaModulePath("library.base.CFunc", CFunc.class), "Function"); 60 | put(new JavaModulePath("library.base.CList", CList.class), "List"); 61 | put(new JavaModulePath("library.io.BasicIO", BasicIO.class), "cmd"); 62 | }}; 63 | } 64 | 65 | public static DObject getObject(JavaModulePath path) { 66 | DObject object = map.get(path); 67 | if (object == null) { 68 | String name = DEFAULT_MODULE_PATHS.get(path); 69 | object = DEFAULT_IMPORTS.get(name); 70 | } 71 | return object; 72 | } 73 | 74 | public static DObject getDefaultImport(String name) { 75 | return DEFAULT_IMPORTS.get(name); 76 | } 77 | 78 | public static void add(JavaModulePath path, Class module) { 79 | if (DEFAULT_MODULE_PATHS.containsKey(path)) 80 | return; 81 | map.put(path, generateFrom(module)); 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /src/main/java/runtime/ReferenceSymbol.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 runtime; 31 | 32 | public final class ReferenceSymbol { 33 | private String name; 34 | private String alias; 35 | private String path; 36 | 37 | public ReferenceSymbol(String name, String alias, String path) { 38 | this.name = name; 39 | this.alias = alias; 40 | this.path = path; 41 | } 42 | 43 | public String getPath() { 44 | return path; 45 | } 46 | 47 | public String getName() { 48 | return name; 49 | } 50 | 51 | public String getAlias() { 52 | return alias; 53 | } 54 | 55 | public boolean hasAlias() { 56 | return alias != null && !alias.isEmpty(); 57 | } 58 | 59 | } 60 | -------------------------------------------------------------------------------- /src/main/java/runtime/ReferenceTable.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 runtime; 31 | 32 | import runtime.imports.ModulePath; 33 | 34 | import java.util.*; 35 | 36 | public final class ReferenceTable { 37 | private List symbols = new LinkedList<>(); 38 | 39 | public ReferenceTable() { 40 | } 41 | 42 | public void add(ReferenceSymbol symbol) { 43 | symbols.add(symbol); 44 | } 45 | 46 | public Set getImportPaths() { 47 | Set list = new HashSet<>(); 48 | for (ReferenceSymbol symbol : symbols) { 49 | try { 50 | list.add(ModulePath.fromPath(symbol.getPath())); 51 | } catch (ClassNotFoundException e) { 52 | e.printStackTrace(); 53 | } 54 | } 55 | return list; 56 | } 57 | 58 | public ReferenceSymbol resolve(String importName) { 59 | Optional op = symbols.stream() 60 | .filter(e -> e.getAlias() == importName) 61 | .findFirst(); 62 | if (op.isPresent()) { 63 | return op.get(); 64 | } 65 | op = symbols.stream() 66 | .filter(e -> e.getName() == importName) 67 | .findFirst(); 68 | if (op.isPresent()) { 69 | return op.get(); 70 | } 71 | return null; 72 | } 73 | 74 | } 75 | -------------------------------------------------------------------------------- /src/main/java/runtime/Runtime.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 runtime; 31 | 32 | 33 | public final class Runtime { 34 | 35 | private Runtime() { 36 | } 37 | 38 | public static void runtime(Class module) throws Throwable { 39 | new ImportEvaluator().evaluate(module); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/main/java/runtime/imports/CafeModulePath.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 runtime.imports; 31 | 32 | import java.io.IOException; 33 | import java.nio.file.FileSystems; 34 | import java.nio.file.Files; 35 | import java.nio.file.Path; 36 | import java.util.Objects; 37 | 38 | public class CafeModulePath extends ModulePath { 39 | private String className; 40 | private String stringPath; 41 | private Path path; 42 | 43 | public CafeModulePath(String path) { 44 | this.className = path.substring(path.lastIndexOf('/') + 1); 45 | this.stringPath = path; 46 | this.path = FileSystems.getDefault() 47 | .getPath(path); 48 | } 49 | 50 | public Path getPath() { 51 | return path; 52 | } 53 | 54 | public String getClassName() { 55 | return className; 56 | } 57 | 58 | public String asString() { 59 | return stringPath; 60 | } 61 | 62 | @Override 63 | public void accept(ImportPathVisitor v) { 64 | v.visit(this); 65 | } 66 | 67 | @Override 68 | public boolean equals(Object o) { 69 | if (this == o) return true; 70 | if (o == null || getClass() != o.getClass()) return false; 71 | CafeModulePath urlPath = (CafeModulePath) o; 72 | try { 73 | return Files.isSameFile(path, urlPath.path); 74 | } catch (IOException e) { 75 | e.printStackTrace(); 76 | return false; 77 | } 78 | } 79 | 80 | @Override 81 | public int hashCode() { 82 | return Objects.hash(path); 83 | } 84 | 85 | @Override 86 | public String toString() { 87 | return "CafeModulePath{" + 88 | "Path='" + stringPath + '\'' + 89 | '}'; 90 | } 91 | } 92 | -------------------------------------------------------------------------------- /src/main/java/runtime/imports/ImportPathVisitor.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 runtime.imports; 31 | 32 | public interface ImportPathVisitor { 33 | void visit(CafeModulePath path); 34 | 35 | void visit(JavaModulePath path); 36 | } 37 | -------------------------------------------------------------------------------- /src/main/java/runtime/imports/JavaModulePath.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 runtime.imports; 31 | 32 | import java.util.Objects; 33 | 34 | public class JavaModulePath extends ModulePath { 35 | private String moduleString; 36 | 37 | public JavaModulePath(String moduleString, Class module) { 38 | this.moduleString = moduleString; 39 | super.module = module; 40 | } 41 | 42 | public static JavaModulePath fromPath(String path) throws ClassNotFoundException { 43 | path = path.replaceAll("/", ".") 44 | .trim(); 45 | path = "library." + path; 46 | return new JavaModulePath(path, Class.forName(path)); 47 | } 48 | 49 | @Override 50 | public void accept(ImportPathVisitor v) { 51 | v.visit(this); 52 | } 53 | 54 | @Override 55 | public boolean equals(Object o) { 56 | if (this == o) return true; 57 | if (o == null || getClass() != o.getClass()) return false; 58 | JavaModulePath otherModule = (JavaModulePath) o; 59 | return moduleString.equals(otherModule.moduleString); 60 | } 61 | 62 | @Override 63 | public int hashCode() { 64 | return Objects.hash(moduleString); 65 | } 66 | 67 | @Override 68 | public String toString() { 69 | return "JavaModulePath{" + 70 | "module='" + moduleString + '\'' + 71 | '}'; 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /src/main/java/runtime/imports/ModulePath.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 runtime.imports; 31 | 32 | import java.io.File; 33 | 34 | public abstract class ModulePath { 35 | Class module; 36 | 37 | public Class getModule() { 38 | return module; 39 | } 40 | 41 | public void setModule(Class module) { 42 | this.module = module; 43 | } 44 | 45 | public static ModulePath fromPath(String path) throws ClassNotFoundException { 46 | File f = new File(path + ".class"); 47 | if (!f.exists()) { 48 | path = path.replaceAll("/", ".") 49 | .trim(); 50 | path = "library." + path; 51 | return new JavaModulePath(path, Class.forName(path)); 52 | } else { 53 | return new CafeModulePath(path); 54 | } 55 | } 56 | 57 | public abstract void accept(ImportPathVisitor v); 58 | 59 | public abstract boolean equals(Object o); 60 | 61 | public abstract int hashCode(); 62 | 63 | 64 | } 65 | -------------------------------------------------------------------------------- /src/main/java/runtime/indy/FunctionInvocationID.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 runtime.indy; 31 | 32 | import library.DFunc; 33 | 34 | import java.lang.invoke.*; 35 | import java.util.Arrays; 36 | 37 | import static java.lang.invoke.MethodType.methodType; 38 | 39 | public final class FunctionInvocationID { 40 | private static final MethodHandle FALLBACK; 41 | 42 | static { 43 | try { 44 | MethodHandles.Lookup lookup = MethodHandles.lookup(); 45 | 46 | FALLBACK = lookup.findStatic( 47 | FunctionInvocationID.class, 48 | "fallback", 49 | methodType(Object.class, FunctionCallSite.class, Object[].class)); 50 | } catch (NoSuchMethodException | IllegalAccessException e) { 51 | throw new Error("Could not bootstrap the required method handles", e); 52 | } 53 | } 54 | 55 | public static class FunctionCallSite extends MutableCallSite { 56 | 57 | final MethodHandles.Lookup callerLookup; 58 | final String name; 59 | 60 | FunctionCallSite(MethodHandles.Lookup callerLookup, String name, MethodType type) { 61 | super(type); 62 | this.callerLookup = callerLookup; 63 | this.name = name; 64 | } 65 | } 66 | 67 | public static CallSite bootstrap(MethodHandles.Lookup caller, String name, MethodType type, Object... bsmArgs) { 68 | FunctionCallSite callSite = new FunctionCallSite(caller, name, type); 69 | MethodHandle fallbackHandle = FALLBACK 70 | .bindTo(callSite) 71 | .asCollector(Object[].class, type.parameterCount()) 72 | .asType(type); 73 | callSite.setTarget(fallbackHandle); 74 | return callSite; 75 | } 76 | 77 | public static Object fallback(FunctionCallSite callSite, Object[] args) throws Throwable { 78 | // System.out.println("Name=>"+ callSite.name); 79 | // for(int i=0;i 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 runtime.indy; 31 | 32 | import library.DFunc; 33 | import library.DObject; 34 | import runtime.DObjectCreator; 35 | 36 | import java.lang.invoke.CallSite; 37 | import java.lang.invoke.ConstantCallSite; 38 | import java.lang.invoke.MethodHandles; 39 | import java.lang.invoke.MethodType; 40 | import java.lang.reflect.Method; 41 | 42 | import static java.lang.invoke.MethodHandles.constant; 43 | import static java.lang.invoke.MethodType.genericMethodType; 44 | 45 | public final class FunctionReferenceID { 46 | public static CallSite bootstrap(MethodHandles.Lookup caller, String name, MethodType type, String moduleClass, int arity, int varargs) throws Throwable { 47 | Class module = caller.lookupClass() 48 | .getClassLoader() 49 | .loadClass(moduleClass); 50 | Method function = module.getDeclaredMethod(name, genericMethodType(arity, varargs == 1) 51 | .changeParameterType(0, DObject.class) 52 | .parameterArray()); 53 | function.setAccessible(true); 54 | return new ConstantCallSite(constant( 55 | DFunc.class, 56 | DObjectCreator.createFunc(caller.unreflect(function)))); 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /src/test/java/compiler/CompileAndCheckTest.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 | import library.DObject; 33 | import org.testng.annotations.Test; 34 | import runtime.DObjectCreator; 35 | 36 | import java.lang.reflect.Method; 37 | 38 | import static org.testng.Assert.assertEquals; 39 | import static testing.Utils.*; 40 | 41 | public class CompileAndCheckTest { 42 | 43 | public static final String SRC = "src/test/resources/"; 44 | public static final DObject thisP = DObjectCreator.create(); 45 | 46 | @Test 47 | public void test_ann_func() throws Throwable { 48 | String folder = SRC + "ann-func-test/"; 49 | compileAndStore(folder + "anonymous_func_export.cafe"); 50 | 51 | Class clazz = compileAndLoad(folder + "anonymous_func_import.cafe"); 52 | execute(clazz); 53 | 54 | //Arrays.stream(clazz.getDeclaredMethods()).forEach(System.out::println); 55 | Method method1 = clazz.getMethod("test", DObject.class); 56 | assertEquals(method1.invoke(null, thisP), "Hello"); 57 | } 58 | 59 | @Test 60 | public void test_ann_func_as_export() throws Throwable { 61 | String folder = SRC + "ann-func-test/as-closure/"; 62 | compileAndStore(folder + "ann_func_export.cafe"); 63 | 64 | Class clazz = compileAndLoad(folder + "ann_func_import.cafe"); 65 | execute(clazz); 66 | 67 | Method method1 = clazz.getMethod("test", DObject.class); 68 | assertEquals(method1.invoke(null, thisP), "inside closure"); 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /src/test/java/compiler/CompileAndExecuteTest.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 | import org.objectweb.asm.ClassReader; 33 | import org.objectweb.asm.util.TraceClassVisitor; 34 | import org.testng.annotations.DataProvider; 35 | import org.testng.annotations.Test; 36 | import testing.Utils; 37 | 38 | import java.io.File; 39 | import java.io.PrintWriter; 40 | import java.util.Iterator; 41 | 42 | import static testing.Utils.compileAndLoad; 43 | import static testing.Utils.execute; 44 | 45 | public class CompileAndExecuteTest { 46 | public static final String SRC = "src/test/resources/compile-and-execute/"; 47 | 48 | @DataProvider(name = "cafe-files") 49 | public static Iterator data() { 50 | return Utils.cafeFilesIn(SRC); 51 | } 52 | 53 | @Test(dataProvider = "cafe-files") 54 | public void call_init_func(File cafeFile) throws Throwable { 55 | System.out.println(cafeFile); 56 | Class clazz = compileAndLoad(cafeFile.getAbsolutePath()); 57 | execute(clazz); 58 | } 59 | 60 | private void trace(byte[] result) { 61 | ClassReader reader = new ClassReader(result); 62 | TraceClassVisitor tracer = new TraceClassVisitor(new PrintWriter(System.out)); 63 | reader.accept(tracer, 0); 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /src/test/java/compiler/CompileTest.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 | import compiler.main.CafeCompiler; 33 | import compiler.main.CompilerResult; 34 | import org.testng.annotations.DataProvider; 35 | import org.testng.annotations.Test; 36 | import testing.Utils; 37 | 38 | import java.io.File; 39 | import java.util.Iterator; 40 | 41 | import static org.testng.Assert.assertTrue; 42 | 43 | public class CompileTest { 44 | public static final String SRC = "src/test/resources/compile/"; 45 | 46 | @DataProvider(name = "cafe-files") 47 | public static Iterator data() { 48 | return Utils.cafeFilesIn(SRC); 49 | } 50 | 51 | @Test(dataProvider = "cafe-files") 52 | public void compile(File cafeFile) { 53 | CafeCompiler compiler = new CafeCompiler(cafeFile.getAbsolutePath()); 54 | CompilerResult result = compiler.compile(); 55 | assertTrue(result.isOk()); 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /src/test/java/compiler/gen/AnnFuncNameGeneratorTest.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.testng.annotations.Test; 33 | 34 | import static org.testng.Assert.assertTrue; 35 | 36 | public class AnnFuncNameGeneratorTest { 37 | 38 | @Test 39 | public static void function_stack() { 40 | AnnFuncNameGenerator gen = new AnnFuncNameGenerator(); 41 | assertTrue(gen.current() 42 | .equals("#_ANN_FUNC_")); 43 | 44 | gen.enter(); 45 | assertTrue(gen.current() 46 | .equals("#_ANN_FUNC_1")); 47 | 48 | gen.next(); 49 | assertTrue(gen.current() 50 | .equals("#_ANN_FUNC_2")); 51 | 52 | gen.enter(); 53 | assertTrue(gen.current() 54 | .equals("#_ANN_FUNC_2_3")); 55 | 56 | gen.next(); 57 | assertTrue(gen.current() 58 | .equals("#_ANN_FUNC_2_4")); 59 | 60 | gen.leave(); 61 | assertTrue(gen.current() 62 | .equals("#_ANN_FUNC_2")); 63 | 64 | gen.next(); 65 | assertTrue(gen.current() 66 | .equals("#_ANN_FUNC_5")); 67 | 68 | gen.leave(); 69 | assertTrue(gen.current() 70 | .equals("#_ANN_FUNC_")); 71 | 72 | gen.leave(); 73 | assertTrue(gen.current() 74 | .equals("#_ANN_FUNC_")); 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /src/test/java/compiler/parser/MainParserTest.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.parser; 31 | 32 | import compiler.ast.Node; 33 | import compiler.main.SourceFileManager; 34 | import compiler.util.Context; 35 | import org.testng.annotations.BeforeMethod; 36 | import org.testng.annotations.DataProvider; 37 | import org.testng.annotations.Test; 38 | import testing.Utils; 39 | 40 | import java.io.File; 41 | import java.util.Iterator; 42 | 43 | import static org.testng.Assert.assertNotNull; 44 | import static org.testng.Assert.assertNull; 45 | 46 | public class MainParserTest { 47 | public static final String SRC = "src/test/resources/parse-test/"; 48 | public static final String ERR = "src/test/resources/parse-test/error_test"; 49 | 50 | private ParserFactory factory; 51 | private SourceFileManager fm; 52 | 53 | @DataProvider(name = "cafe-files") 54 | public static Iterator data() { 55 | return Utils.cafeFilesIn(SRC); 56 | } 57 | 58 | @DataProvider(name = "cafe-error-files") 59 | public static Iterator error_data() { 60 | return Utils.cafeFilesIn(ERR); 61 | } 62 | 63 | @BeforeMethod 64 | public void init() { 65 | Context context = new Context(); 66 | factory = ParserFactory.instance(context); 67 | fm = SourceFileManager.instance(context); 68 | } 69 | 70 | @Test(dataProvider = "cafe-files") 71 | public void test_no_error(File file) { 72 | fm.setSourceFile(file); 73 | Parser parser = factory.newParser(ParserType.MAINPARSER, fm.asCharList()); 74 | Node n = parser.parse(); 75 | assertNotNull(n); 76 | } 77 | 78 | @Test(dataProvider = "cafe-error-files") 79 | public void test_error(File file) { 80 | fm.setSourceFile(file); 81 | Parser parser = factory.newParser(ParserType.MAINPARSER, fm.asCharList()); 82 | Node n = parser.parse(); 83 | assertNull(n); 84 | } 85 | } 86 | -------------------------------------------------------------------------------- /src/test/java/testing/Utils.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 testing; 31 | 32 | import runtime.CafeClassLoader; 33 | import runtime.ImportEvaluator; 34 | 35 | import java.io.File; 36 | import java.util.Iterator; 37 | import java.util.LinkedList; 38 | import java.util.List; 39 | 40 | public class Utils { 41 | 42 | public static Class compileAndLoad(String filePath) { 43 | CafeClassLoader loader = new CafeClassLoader(); 44 | return loader.compileAndLoadModule(filePath); 45 | } 46 | 47 | public static void compileAndStore(String filePath) { 48 | CafeClassLoader loader = new CafeClassLoader(Utils.class.getClassLoader()); 49 | loader.compileAndStoreModule(filePath, null); 50 | } 51 | 52 | public static void compileAndStore(String filePath, String destination) { 53 | CafeClassLoader loader = new CafeClassLoader(Utils.class.getClassLoader()); 54 | loader.compileAndStoreModule(filePath, destination); 55 | } 56 | 57 | public static Iterator cafeFilesIn(String path) { 58 | List data = new LinkedList<>(); 59 | File[] files = new File(path).listFiles((dir, name) -> name.endsWith(".cafe")); 60 | for (File file : files) { 61 | data.add(new Object[]{file}); 62 | } 63 | return data.iterator(); 64 | } 65 | 66 | public static void execute(Class clazz) throws Throwable { 67 | ImportEvaluator evaluator = new ImportEvaluator(); 68 | evaluator.evaluate(clazz); 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /src/test/resources/ann-func-test/anonymous_func_export.cafe: -------------------------------------------------------------------------------- 1 | export var a = func(){ 2 | return "Hello"; 3 | }; -------------------------------------------------------------------------------- /src/test/resources/ann-func-test/anonymous_func_import.cafe: -------------------------------------------------------------------------------- 1 | import a from "./src/test/resources/ann-func-test/anonymous_func_export"; 2 | 3 | export func test(){ 4 | return a(); 5 | } -------------------------------------------------------------------------------- /src/test/resources/ann-func-test/as-closure/ann_func_export.cafe: -------------------------------------------------------------------------------- 1 | var a = func(){ 2 | return func(){ 3 | return "inside closure"; 4 | }; 5 | }; 6 | 7 | export var z = a(); 8 | -------------------------------------------------------------------------------- /src/test/resources/ann-func-test/as-closure/ann_func_import.cafe: -------------------------------------------------------------------------------- 1 | import z as a from "./src/test/resources/ann-func-test/as-closure/ann_func_export"; 2 | 3 | export func test(){ 4 | return a(); 5 | } -------------------------------------------------------------------------------- /src/test/resources/compile-and-execute/ann_func.cafe: -------------------------------------------------------------------------------- 1 | var a = func(){ 2 | return "Hello"; 3 | }; 4 | 5 | a(); -------------------------------------------------------------------------------- /src/test/resources/compile-and-execute/bind_args.cafe: -------------------------------------------------------------------------------- 1 | func a(b,c){ 2 | cmd.println(b); 3 | } 4 | 5 | var z = {h:"inside z"}; 6 | var q = a.bind(z,1); 7 | q(3); -------------------------------------------------------------------------------- /src/test/resources/compile-and-execute/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(); -------------------------------------------------------------------------------- /src/test/resources/compile-and-execute/default_import_object_test.cafe: -------------------------------------------------------------------------------- 1 | cmd.println(Object.__proto__); 2 | cmd.println(Function.__proto__); -------------------------------------------------------------------------------- /src/test/resources/compile-and-execute/for_loop.cafe: -------------------------------------------------------------------------------- 1 | for(var i=0;i<10;i=i+1){ 2 | var j; 3 | } -------------------------------------------------------------------------------- /src/test/resources/compile-and-execute/function_call.cafe: -------------------------------------------------------------------------------- 1 | func A(){ 2 | cmd.println("Hello"); 3 | } 4 | 5 | A(); -------------------------------------------------------------------------------- /src/test/resources/compile-and-execute/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 | cmd.println("Size: "+l.size()); 24 | 25 | -------------------------------------------------------------------------------- /src/test/resources/compile-and-execute/missing_pop_if_else.cafe: -------------------------------------------------------------------------------- 1 | var acc = { 2 | amount: 0, 3 | updateBal: func(b){ 4 | }, 5 | withdraw: func(b){ 6 | if(this.amount < b){ 7 | cmd.println("Insufficient Balance"); 8 | cmd.println("Please check"); 9 | } else { 10 | cmd.print("Hello"); 11 | this.updateBal(-b); 12 | } 13 | } 14 | }; 15 | 16 | acc.withdraw(2000); -------------------------------------------------------------------------------- /src/test/resources/compile/list.cafe: -------------------------------------------------------------------------------- 1 | var x = {}; 2 | var l = [1,[2,3],"string", x]; -------------------------------------------------------------------------------- /src/test/resources/parse-test/error_test/for_loop_error.cafe: -------------------------------------------------------------------------------- 1 | for(var i=0 i<10;){ 2 | 3 | } -------------------------------------------------------------------------------- /src/test/resources/parse-test/error_test/slice_subscript_error.cafe: -------------------------------------------------------------------------------- 1 | var l = [1,[2,3],"string", x]; 2 | var r1 = l[0:]; 3 | l[0:1] = r; -------------------------------------------------------------------------------- /src/test/resources/parse-test/if_else.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 | cmd.println("Please check"); 18 | } else { 19 | cmd.print("Hello"); 20 | updateBal(-b); 21 | } 22 | } 23 | }; 24 | 25 | cmd.println(acc.amount); 26 | acc.init(); 27 | acc.checkBalance(); 28 | acc.deposit(1000); 29 | acc.withdraw(2000); 30 | acc.checkBalance(); -------------------------------------------------------------------------------- /src/test/resources/parse-test/slice_subscript.cafe: -------------------------------------------------------------------------------- 1 | var x = {a:10}; 2 | var l = [1,[2,3],"string", x]; 3 | cmd.println(x[z]); 4 | # var r = l[a:b()]; 5 | var r1 = l[0:4]; 6 | var r = l[2:b()]; 7 | l[0:1] = r; --------------------------------------------------------------------------------