Commit e4fd0d13 authored by A. Borzov's avatar A. Borzov
Browse files

Перенес на android.

parents
*.iml
.gradle
/local.properties
/.idea/caches
/.idea/libraries
/.idea/modules.xml
/.idea/workspace.xml
/.idea/navEditor.xml
/.idea/assetWizardSettings.xml
.DS_Store
/build
/captures
.externalNativeBuild
.cxx
local.properties
# Default ignored files
/shelf/
/workspace.xml
kotlin_cpp
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CompilerConfiguration">
<bytecodeTargetLevel target="11" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="GradleMigrationSettings" migrationVersion="1" />
<component name="GradleSettings">
<option name="linkedExternalProjectsSettings">
<GradleProjectSettings>
<option name="testRunner" value="PLATFORM" />
<option name="distributionType" value="DEFAULT_WRAPPED" />
<option name="externalProjectPath" value="$PROJECT_DIR$" />
<option name="gradleJvm" value="11" />
<option name="modules">
<set>
<option value="$PROJECT_DIR$" />
<option value="$PROJECT_DIR$/app" />
</set>
</option>
<option name="resolveModulePerSourceSet" value="false" />
<option name="useQualifiedModuleNames" value="true" />
</GradleProjectSettings>
</option>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="RemoteRepositoriesConfiguration">
<remote-repository>
<option name="id" value="central" />
<option name="name" value="Maven Central repository" />
<option name="url" value="https://repo1.maven.org/maven2" />
</remote-repository>
<remote-repository>
<option name="id" value="jboss.community" />
<option name="name" value="JBoss Community repository" />
<option name="url" value="https://repository.jboss.org/nexus/content/repositories/public/" />
</remote-repository>
<remote-repository>
<option name="id" value="BintrayJCenter" />
<option name="name" value="BintrayJCenter" />
<option name="url" value="https://jcenter.bintray.com/" />
</remote-repository>
<remote-repository>
<option name="id" value="Google" />
<option name="name" value="Google" />
<option name="url" value="https://dl.google.com/dl/android/maven2/" />
</remote-repository>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_11" default="true" project-jdk-name="11" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/build/classes" />
</component>
<component name="ProjectType">
<option name="id" value="Android" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>
\ No newline at end of file
/build
\ No newline at end of file
plugins {
id 'com.android.application'
id 'kotlin-android'
}
android {
compileSdkVersion 30
buildToolsVersion "30.0.3"
defaultConfig {
applicationId "ru.nsu.fit.borzov.kotlin_cpp"
minSdkVersion 16
targetSdkVersion 30
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
externalNativeBuild {
cmake {
cppFlags ""
}
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
externalNativeBuild {
cmake {
path "src/main/cpp/CMakeLists.txt"
version "3.10.2"
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
}
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'androidx.core:core-ktx:1.3.2'
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'com.google.android.material:material:1.3.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
}
\ No newline at end of file
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html
# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}
# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable
# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
\ No newline at end of file
package ru.nsu.fit.borzov.kotlin_cpp
import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.ext.junit.runners.AndroidJUnit4
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.Assert.*
/**
* Instrumented test, which will execute on an Android device.
*
* See [testing documentation](http://d.android.com/tools/testing).
*/
@RunWith(AndroidJUnit4::class)
class ExampleInstrumentedTest {
@Test
fun useAppContext() {
// Context of the app under test.
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
assertEquals("ru.nsu.fit.borzov.kotlin_cpp", appContext.packageName)
}
}
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="ru.nsu.fit.borzov.kotlin_cpp">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Kotlin_cpp">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
\ No newline at end of file
# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html
# Sets the minimum version of CMake required to build the native library.
cmake_minimum_required(VERSION 3.10.2)
# Declares and names the project.
project("kotlin_cpp")
# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds them for you.
# Gradle automatically packages shared libraries with your APK.
add_library( # Sets the name of the library.
native-lib
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
native-lib.cpp )
# Searches for a specified prebuilt library and stores the path as a
# variable. Because CMake includes system libraries in the search path by
# default, you only need to specify the name of the public NDK library
# you want to add. CMake verifies that the library exists before
# completing its build.
find_library( # Sets the name of the path variable.
log-lib
# Specifies the name of the NDK library that
# you want CMake to locate.
log )
# Specifies libraries CMake should link to your target library. You
# can link multiple libraries, such as libraries you define in this
# build script, prebuilt third-party libraries, or system libraries.
target_link_libraries( # Specifies the target library.
native-lib
# Links the target library to the log library
# included in the NDK.
${log-lib} )
\ No newline at end of file
#include <jni.h>
#include <string>
extern "C" JNIEXPORT jstring JNICALL
Java_ru_nsu_fit_borzov_kotlin_1cpp_MainActivity_stringFromJNI(
JNIEnv* env,
jobject /* this */) {
std::string hello = "Hello from C++";
return env->NewStringUTF(hello.c_str());
}
\ No newline at end of file
package ru.nsu.fit.borzov.kotlin_cpp
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.TextView
import ru.nsu.fit.borzov.kotlin_cpp.edge.machine.TaskExecutor
import ru.nsu.fit.borzov.kotlin_cpp.edge.user.while1
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// Example of a call to a native method
//findViewById<TextView>(R.id.sample_text).text = stringFromJNI()
val t = while1();
val ex = TaskExecutor(t);
ex.run();
}
/**
* A native method that is implemented by the 'native-lib' native library,
* which is packaged with this application.
*/
external fun stringFromJNI(): String
companion object {
// Used to load the 'native-lib' library on application startup.
init {
System.loadLibrary("native-lib")
}
}
}
\ No newline at end of file
package ru.nsu.fit.borzov.kotlin_cpp.edge.lang
data class CodeRow(val opId: Int, val varIds : MutableList<Int>)
\ No newline at end of file
package ru.nsu.fit.borzov.kotlin_cpp.edge.lang
open class LVariable(val id: Int)
class LBool(id: Int) : LCBool(false, id)
class LDouble(id: Int) : LCDouble(0.0,id)
class LInt(id: Int) : LCInt(0,id)
open class LCBool(var value: Boolean, id: Int) : LVariable(id)
open class LCDouble(var value: Double, id: Int) : LVariable(id)
open class LCInt(var value: Int, id: Int) : LVariable(id)
\ No newline at end of file
package ru.nsu.fit.borzov.kotlin_cpp.edge.lang
import ru.nsu.fit.borzov.kotlin_cpp.edge.lang.OpEnum.*
class Task {
//var variables = mutableMapOf<MDouble, Int>()
var constants = mutableMapOf<LVariable,Int>()
var stack = mutableListOf<LVariable>()
var operations = mutableListOf<CodeRow>()
fun While(condition: () -> LBool, block: () -> Unit) {
val stackStart = _getConstInt(stack.size)
val whilePos = _getConstInt(operations.size - 1);
var conditionResult = condition()
conditionResult.not()
val ifJumpPos = operations.size
operations.add(CodeRow(IfJump.id, mutableListOf(conditionResult.id, -1)))
block()
operations.add(CodeRow(Jump.id, mutableListOf(whilePos)))
val varIds = operations[ifJumpPos].varIds;
varIds[varIds.size - 1] = _getConstInt(operations.size - 1);
operations.add(CodeRow(PopStack.id, mutableListOf(stackStart)))
}
private fun _getConstBool(b: Boolean): Int {
return constants.getOrPut(LCBool(b,-(constants.size + 1)), { -(constants.size + 1) })
}
private fun _getConstDouble(b: Double): Int {
return constants.getOrPut(LCDouble(b,-(constants.size + 1)), { -(constants.size + 1) })
}
private fun _getConstInt(b: Int): Int {
return constants.getOrPut(LCInt(b,-(constants.size + 1)), { -(constants.size + 1) })
}
fun createBool(b: Boolean): LBool {
operations.add(CodeRow(CreateBool.id, mutableListOf(_getConstBool(b))));
val toReturn = LBool(stack.size);
stack.add(toReturn)
return toReturn;
}
fun createInt(i: Int): LInt {
operations.add(CodeRow(CreateInt.id, mutableListOf(_getConstInt(i))));
val toReturn = LInt(stack.size);
stack.add(toReturn)
return toReturn;
}
fun createDouble(d: Double): LDouble {
operations.add(CodeRow(CreateDouble.id, mutableListOf(_getConstDouble(d))));
val toReturn = LDouble(stack.size);
stack.add(toReturn)
return toReturn;
}
fun LDouble.plus(b: LDouble) {
operations.add(CodeRow(DoublePlus.id, mutableListOf(this.id, b.id)));
}
fun LDouble.plus(b: Double) {
val id = _getConstDouble(b);
operations.add(CodeRow(DoublePlus.id, mutableListOf(this.id, id)));
}
fun LDouble.minus(b: LDouble) {
operations.add(CodeRow(DoubleMinus.id, mutableListOf(this.id, b.id)));
}
fun LDouble.moreThan(b: LDouble) : LBool {
val result = createBool(false);
operations.add(CodeRow(DoubleMoreThen.id, mutableListOf(result.id, this.id, b.id)))
return result;
}
fun LDouble.lessThan(b: LDouble) : LBool {
val result = createBool(false);
operations.add(CodeRow(DoubleLessThen.id, mutableListOf(result.id, this.id, b.id)))
return result;
}
fun LDouble.lessThan(d: Double) : LBool {
return this.lessThan(LDouble(_getConstDouble(d)));
}
fun LDouble.Equals(b: LDouble) : LBool {
val result = createBool(false);
operations.add(CodeRow(Equals.id, mutableListOf(result.id, this.id, b.id)))
return result;
}
fun LBool.not() : LBool {
operations.add(CodeRow(BoolNot.id, mutableListOf(this.id)))
return this;
}
fun If(condition: () -> LBool, block: () -> Unit) {
val stackStart = _getConstInt(stack.size)
val conditionResult = condition()
conditionResult.not();
val ifJumpPos = operations.size
operations.add(CodeRow(IfJump.id, mutableListOf(conditionResult.id, -1)))
block()
val varIds = operations[ifJumpPos].varIds;
varIds[varIds.size - 1] = _getConstInt(operations.size - 1)//createInt(operations.size - 1).id;
operations.add(CodeRow(PopStack.id, mutableListOf(stackStart)))
}
fun printDouble(b: LDouble) {
operations.add(CodeRow(PrintDouble.id, mutableListOf(b.id)))
}
}
enum class OpEnum(val id: Int) {
DoublePlus(0),
DoubleMinus(1),
IfJump(2),
DoubleMoreThen(3),
PrintDouble(4),
Jump(5),
Equals(6),
BoolNot(7),
PopStack(8),
CreateDouble(9),
CreateInt(10),
CreateBool(11),
DoubleLessThen(12)
}
\ No newline at end of file
package ru.nsu.fit.borzov.kotlin_cpp.edge.machine
import ru.nsu.fit.borzov.kotlin_cpp.edge.lang.LCBool
import ru.nsu.fit.borzov.kotlin_cpp.edge.lang.LCDouble
import ru.nsu.fit.borzov.kotlin_cpp.edge.lang.LCInt
import ru.nsu.fit.borzov.kotlin_cpp.edge.lang.LVariable
open class MVariable() {
constructor(lvar: LVariable): this() {
when {
lvar is LCDouble -> d = lvar.value
lvar is LCBool -> b = lvar.value
lvar is LCInt -> i = lvar.value
}
}
constructor(i: Int): this() {
this.i = i
}
constructor(b: Boolean): this() {
this.b = b
}
constructor(d: Double): this() {
this.d = d
}
var i: Int = 0
var d: Double = 0.0
var b: Boolean = false
}
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment