All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cn.wumoe.hime.module.TimeModule.kt Maven / Gradle / Ivy

Go to download

This is the interpreter of Hime language, a dialect of Lisp, run on JVM platform.

There is a newer version: 2.7.4
Show newest version
package cn.wumoe.hime.module

import cn.wumoe.hime.api.scripting.HimeContext
import cn.wumoe.hime.inter.Module
import cn.wumoe.hime.inter.Function
import cn.wumoe.hime.lexer.Num
import cn.wumoe.hime.lexer.Tag
import cn.wumoe.hime.lexer.Token
import cn.wumoe.hime.lexer.Word
import java.text.SimpleDateFormat
import java.util.*

class TimeModule : Module("hime.time") {
    override fun init(context: HimeContext) {
        addFunction(Time())      // time
        addFunction(FormatTime())   // time-format
        addFunction(ParseTime())    // time-parse
    }

    class Time : Function("time") {
        override fun call(pars: Array): Token {
            return Num.toNum(Date().time)
        }
    }

    class FormatTime : Function("time-format") {
        override fun call(pars: Array): Token {
            return if (pars.size >= 2 && pars[0] is Word && pars[1] is Num) {
                val sdf = SimpleDateFormat((pars[0] as Word).lexeme)
                Word(
                    sdf.format(Date((pars[1] as Num).value.toLong())),
                    Tag.STR
                )
            } else
                Word.NIL
        }
    }

    class ParseTime : Function("time-parse") {
        override fun call(pars: Array): Token {
            return if (pars.size >= 2 && pars[0] is Word && pars[1] is Word) {
                val sdf = SimpleDateFormat((pars[0] as Word).lexeme)
                Num.toNum(sdf.parse((pars[1] as Word).lexeme).time)
            } else
                Word.NIL
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy