Skip to content

Commit

Permalink
BOOL & TEST: some fixes and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
egorklementev committed Feb 27, 2022
1 parent d105ad4 commit b607141
Show file tree
Hide file tree
Showing 14 changed files with 49 additions and 127 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
<br>

[![Gradle Build](https://github.com/polystat/j2eo/actions/workflows/gradle-build.yml/badge.svg)](https://github.com/polystat/j2eo/actions/workflows/gradle-build.yml)
![LINE](https://img.shields.io/badge/line--coverage-41.57%25-orange.svg)
![BRANCH](https://img.shields.io/badge/branch--coverage-31.03%25-red.svg)
![COMPLEXITY](https://img.shields.io/badge/complexity-5.74-brightgreen.svg)
![LINE](https://img.shields.io/badge/line--coverage-30,97%25-red.svg)
![BRANCH](https://img.shields.io/badge/branch--coverage-21,13%25-red.svg)
![COMPLEXITY](https://img.shields.io/badge/complexity-6,69-brightgreen.svg)

This is a translator of **Java** programming language to [EOLANG](https://www.eolang.org) programming language.

Expand Down
1 change: 1 addition & 0 deletions src/main/java/translator/Declarations.kt
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ fun decodePrimitiveType(type: PrimitiveType): String {
return when (type.typeCode) {
TokenCode.Int -> TokenCodes.PRIM__INT.value
TokenCode.Float -> TokenCodes.PRIM__FLOAT.value
TokenCode.Boolean -> TokenCodes.PRIM__BOOLEAN.value
else -> throw IllegalArgumentException("Type code " + type.typeCode + " is not supported")
}
}
Expand Down
25 changes: 15 additions & 10 deletions src/main/java/translator/Literals.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ package translator

import arrow.core.None
import eotree.*
import eotree.data.EOData
import eotree.data.EOFloatData
import eotree.data.EOIntData
import eotree.data.EOStringData
import eotree.data.*
import lexer.TokenCode
import tree.Expression.Primary.Literal
import tree.Type.PrimitiveType
Expand All @@ -14,16 +11,24 @@ import util.TokenCodes
fun mapLiteral(literal: Literal): EOObject =
when (literal.code) {
TokenCode.StringLiteral -> generateEOData(
listOf(TokenCodes.CLASS__STRING.value, "constructor2").eoDot(),
EOStringData(literal.value as String)
listOf(TokenCodes.CLASS__STRING.value, "constructor2").eoDot(),
EOStringData(literal.value as String)
)
TokenCode.FloatingLiteral -> generateEOData(
listOf(TokenCodes.PRIM__FLOAT.value, "constructor2").eoDot(),
EOFloatData((literal.value as String).toFloat())
listOf(TokenCodes.PRIM__FLOAT.value, "constructor2").eoDot(),
EOFloatData((literal.value as String).toFloat())
)
TokenCode.IntegerLiteral -> generateEOData(
listOf(TokenCodes.PRIM__INT.value, "constructor2").eoDot(),
EOIntData((literal.value as String).toInt())
listOf(TokenCodes.PRIM__INT.value, "constructor2").eoDot(),
EOIntData((literal.value as String).toInt())
)
TokenCode.True -> generateEOData(
listOf(TokenCodes.PRIM__BOOLEAN.value, "constructor2").eoDot(),
EOBoolData(true)
)
TokenCode.False -> generateEOData(
listOf(TokenCodes.PRIM__BOOLEAN.value, "constructor2").eoDot(),
EOBoolData(false)
)
else -> throw IllegalArgumentException("Mapping of type ${literal.javaClass.simpleName} is not supported.")
}
Expand Down
29 changes: 27 additions & 2 deletions src/main/java/translator/preprocessor/Preprocessor.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package translator.preprocessor

import lexer.TokenCode
import tree.Compilation.CompilationUnit
import tree.Compilation.SimpleCompilationUnit
import tree.Compilation.TopLevelComponent
Expand All @@ -12,9 +13,13 @@ import tree.InitializerSimple
import tree.Statement.BlockStatement
import tree.Statement.Statement
import tree.Statement.StatementExpression
import tree.Type.PrimitiveType
import tree.Type.TypeName
import util.TokenCodes
import util.collectPrimitivePackages
import java.util.*
import kotlin.collections.HashMap
import kotlin.collections.HashSet

/**
* @property classNames
Expand All @@ -23,8 +28,9 @@ import util.collectPrimitivePackages
*/
data class PreprocessorState(
val classNames: HashMap<String, String> = hashMapOf(
"Object" to TokenCodes.CLASS__OBJECT.value,
"System" to TokenCodes.CLASS__SYSTEM.value,
"Object" to TokenCodes.CLASS__OBJECT.value,
"System" to TokenCodes.CLASS__SYSTEM.value,
"int" to TokenCodes.PRIM__INT.value
),
val stdClassesNeededForAlias: HashSet<String> = hashSetOf(
TokenCodes.CLASS__OBJECT.value,
Expand Down Expand Up @@ -144,6 +150,15 @@ private fun preprocessMethodDecl(state: PreprocessorState, methodDecl: MethodDec
try {
methodDecl.methodBody.block.blockStatements
.map { blockStmt: BlockStatement -> preprocessBlockStmt(state, blockStmt) }
when (methodDecl.type) {
is PrimitiveType ->
{
tryAddClassForAliases(
state, convertPrimTokenCode((methodDecl.type as PrimitiveType).typeCode)
)
}
else -> {}
}
} catch (e: NullPointerException) {
/* Ignore it */
}
Expand Down Expand Up @@ -240,3 +255,13 @@ private fun tryAddClassForAliases(state: PreprocessorState, className: String) {
state.stdClassesForCurrentAlias.add(className)
}
}

/**
* Converts token code name of type to the stdlib EO class.
* Example: TokenCode.Int -> 'prim__int'
*
* @param tokenCode the token code of primitive type
*/
private fun convertPrimTokenCode(tokenCode: TokenCode) : String {
return "prim__${tokenCode.name.lowercase(Locale.getDefault())}"
}

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

7 changes: 3 additions & 4 deletions src/test/resources/test_ready/SimpleTest.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
public class SimpleTest {

public static void main(String[] args) {
int a = 5;
int b = (-a);
System.out.println(b);
boolean a = true;
boolean b = false;
System.out.println("passed");
}
}

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

0 comments on commit b607141

Please sign in to comment.