Skip to content

Releases: imwithye/LJVM

Pre-Release 0.2.1 of Lucy Lang

28 Feb 19:49
Compare
Choose a tag to compare
Pre-release

Version 0.2.1 - Pre-Release of Lucy Lang

  • Fix linker error
  • Add array type (experimental)
  • Add example code, minesweeper game
usage: lucy [options] target
 -c,--compile <file>    compile lucy source code to lucy X bit code
 -d,--dump <file>       dump lucy X object to human readable form
 -h,--help              print the help message and exit
 -o,--output <output>   output file path
 -v,--version           print the version information and exit
package "game"

import "util"
import "std"
import "message"

func valid_index(i, j, size) {
    index = i * size + j
    if index >= 0 && index < size * size {
        return index
    } else {
        return -1
    }
}

func init_map(map, size) {
    var i = 0, j = 0, total = 0
    while total < size {
        i = 0
        j = 0
        while i < size {
            while j < size {
                var index = i * size + j
                if util::random(size) && total < size && map[index] != 1 {
                    map[index] = -1
                    total = total + 1
                }
                j = j + 1
            }
            i = i + 1
            j = 0
        }
    }
    i = 0
    j = 0
    while i < size {
        while j < size {
            count = 0
            if valid_index(i-1, j-1, size) != -1 {
                if map[valid_index(i-1, j-1, size)] == -1 {
                    count = count + 1
                }
            }
            if valid_index(i-1, j, size) != -1 {
                if map[valid_index(i-1, j, size)] == -1 {
                    count = count + 1
                }
            }
            if valid_index(i-1, j+1, size) != -1 {
                if map[valid_index(i-1, j+1, size)] == -1 {
                    count = count + 1
                }
            }
            if valid_index(i, j-1, size) != -1 {
                if map[valid_index(i, j-1, size)] == -1 {
                    count = count + 1
                }
            }
            if valid_index(i, j+1, size) != -1 {
                if map[valid_index(i, j+1, size)] == -1 {
                    count = count + 1
                }
            }
            if valid_index(i+1, j-1, size) != -1 {
                if map[valid_index(i+1, j-1, size)] == -1 {
                    count = count + 1
                }
            }
            if valid_index(i+1, j, size) != -1 {
                if map[valid_index(i+1, j, size)] == -1 {
                    count = count + 1
                }
            }
            if valid_index(i+1, j+1, size) != -1 {
                if map[valid_index(i+1, j+1, size)] == -1 {
                    count = count + 1
                }
            }
            if map[i*size+j] != -1 {
                map[i*size+j] = count
            }
            j = j + 1
        }
        i = i + 1
        j = 0
    }
    return map
}

func print_map(map, size) {
    j = -1
    while j < size {
        if j >= 0 {
            std::print(util::formatter(2, j))
        } else {
            std::print(util::formatter(2, ""))
        }
        std::print(" ")
        j = j + 1
    }
    std::print("\n")
    i = 0
    j = -1
    while i < size {
        while j < size {
            if j == -1 {
                std::print(util::formatter(2, i))
            } else {
                std::print(util::formatter(2, map[i * size + j]))
            }
            std::print(" ")
            j = j + 1
        }
        std::print("\n")
        i = i + 1
        j = -1
    }
}

func print_and_next(cover_map, size, found) {
    print_map(cover_map, size)
    std::print("Total mines: " + std::string(size) + " You flaged " + std::string(found) + "\n")
    row = -1
    while row < 0 || row >= size {
        std::print("Input position(row): ")
        row = std::number(std::input())
    }
    col = -1
    while col < 0 || col >= size {
        std::print("Input position(col): ")
        col = std::number(std::input())
    }
    choice = ""
    while !(choice == "O" || choice == "F" || choice == "U" || choice == "C") {
        std::print("Input choice([O]pen, [F]lag, [U]nflag, [C]ancel: ")
        choice = std::input()
    }
    return [row, col, choice]
}

func open(map, cover_map, size, row, col) {
    index = row * size + col
    if map[index] == -1 {
        return [true, cover_map]
    }
    if map[index] == 0 {
        cover_map[index] = " "
        left = (row - 1) * size + col
        if left >= 0 && left < size*size {
            if map[left] == 0 && cover_map[left] != " " {
                result = open(map, cover_map, size, row - 1, col)
                cover_map = result[1]
            }
            if map[left] != -1 && cover_map[left] != " " {
                cover_map[left] = map[left]
            }
        }
        right = (row + 1) * size + col
        if right >= 0 && right < size*size {
            if map[right] == 0 && cover_map[right] != " " {
                result = open(map, cover_map, size, row + 1, col)
                cover_map = result[1]
            }
            if map[right] != -1 && cover_map[right] != " " {
                cover_map[right] = map[right]
            }
        }
        up = row * size + (col - 1)
        if up >= 0 && up < size*size {
            if map[up] == 0 && cover_map[up] != " " {
                result = open(map, cover_map, size, row, col - 1)
                cover_map = result[1]
            }
            if map[up] != -1 && cover_map[up] != " " {
                cover_map[up] = map[up]
            }
        }
        down = row * size + (col + 1)
        if down >= 0 && down < size*size {
            if map[down] == 0 && cover_map[down] != " " {
                result = open(map, cover_map, size, row, col + 1)
                cover_map = result[1]
            }
            if map[down] != -1 && cover_map[down] != " " {
                cover_map[down] = map[down]
            }
        }
        return [false, cover_map]
    } else {
        cover_map[index] = map[index]
        return [false, cover_map]
    }
}

func flag(cover_map, size, row, col) {
    index = row * size + col
    if cover_map[index] == "^" {
        cover_map[index] = "F"
        return [true, cover_map]
    } else {
        return [false, cover_map]
    }
}

func unflag(cover_map, size, row, col) {
    index = row * size + col
    if cover_map[index] == "F" {
        cover_map[index] = "^"
        return [true, cover_map]
    } else {
        return [false, cover_map]
    }
}

func check_game(map, cover_map, size) {
    total = size
    correct = 0
    incorrect = 0
    i = 0
    j = 0
    while i < size {
        while j < size {
            index = i * size + j
            if map[index] == -1 && cover_map[index] == "F" {
                correct = correct + 1
            }
            if map[index] == -1 && cover_map[index] != "F" {
                cover_map[index] = "*"
            }
            if map[index] != -1 && cover_map[index] == "F" {
                cover_map[index] = "W"
                incorrect = incorrect + 1
            }
            j = j + 1
        }
        j = 0
        i = i + 1
    }
    if correct < total {
        return [false, cover_map]
    } else {
        return [true, cover_map]
    }
}

func start(size) {
    map = init_map(util::init_array(size * size, 0), size)
    cover_map = util::init_array(size * size, "^")
    total = size
    found = 0
    game_end = false
    while found < size && !game_end {
        next_move = print_and_next(cover_map, size, found)
        if next_move[2] == "O" {
            result = open(map, cover_map, size, next_move[0], next_move[1])
            game_end = result[0]
            cover_map = result[1]
        }
        if next_move[2] == "F" {
            result = flag(cover_map, size, next_move[0], next_move[1])
            if result[0] {
                found = found + 1
            }
            cover_map = result[1]
        }
        if next_move[2] == "U" {
            result = unflag(cover_map, size, next_move[0], next_move[1])
            if result[0] {
                found = found - 1
            }
            cover_map = result[1]
        }
        message::clear()
    }
    result = check_game(map, cover_map, size)
    print_map(result[1], size)
    if result[0] {
        std::print("You win!\n")
        std::print("Thanks for trying! http://lucy-lang.org\n")
    } else {
        std::print("You lose!\n")
        std::print("Thanks for trying! http://lucy-lang.org\n")
    }
}

Pre-Release 0.2.0 of Lucy Lang

28 Feb 06:26
Compare
Choose a tag to compare
Pre-release

Version 0.2.0 - Pre-Release of Lucy Lang

  • Separate the linker and compiler
  • Add package keyword. Modules are formed by packages.
  • main function should be put under main package.
  • import from user defined module is possible now.
usage: lucy [options] target
 -c,--compile <file>    compile lucy source code to lucy X bit code
 -d,--dump <file>       dump lucy X object to human readable form
 -h,--help              print the help message and exit
 -o,--output <output>   output file path
 -v,--version           print the version information and exit
package "main"

import "std"

func power(n, exp) {
    result = 1
    while exp > 0 {
        result = result * n
        exp = exp - 1
    }
    return result
}

func factorial(n) {
    if n <= 1 {
        return 1
    } else {
        return n * factorial(n-1)
    }
}

func sin(x) {
    var upper = 50, i = 0, sum = 0, sign = true
    while i < upper {
        if sign {
            sum = sum + power(x, 1 + 2 * i) / factorial(1 + 2 * i)
        } else {
            sum = sum - power(x, 1 + 2 * i) / factorial(1 + 2 * i)
        }
        sign = !sign
        i = i + 1
    }
    return sum
}

func cos(x) {
    var upper = 50, i = 0, sum = 0, sign = true
    while i < upper {
        if sign {
            sum = sum + power(x, 2 * i) / factorial(2 * i)
        } else {
            sum = sum - power(x, 2 * i) / factorial(2 * i)
        }
        sign = !sign
        i = i + 1
    }
    return sum
}

func main() {
    print("input n: ")
    n = input()
    while n != "end" {
        n = number(n)
        print("sin(" + string(n) + ") = " + string(sin(n)) + "\n")
        print("cos(" + string(n) + ") = " + string(cos(n)) + "\n")
        print("input n: ")
        n = input()
    }
}

Pre-Release 0.1.1 of Lucy Lang

25 Feb 17:44
Compare
Choose a tag to compare
Pre-release

Version 0.1.1 - Pre-Release of Lucy Lang

  • Add - operator. i.e. -1, -sin(x).
  • Fix call statement bug
  • Add NotExpr node
  • Add system library and import keyword
  • Add Lucy Example Code
$ java -jar target/lucy-0.1.1.jar
usage: lucy [options] target
 -c,--compile <file>    compile lucy source code to lucy X bit code
 -d,--dump <file>       dump lucy X object to human readable form
 -h,--help              print the help message and exit
 -o,--output <output>   output file path
 -r,--run <file>        run lucy X bit code
 -t,--token <file>      dump lucy tokens
 -v,--version           print the version information and exit
import "std"

func power(n, exp) {
    result = 1
    while exp > 0 {
        result = result * n
        exp = exp - 1
    }
    return result
}

func factorial(n) {
    if n <= 1 {
        return 1
    } else {
        return n * factorial(n-1)
    }
}

func sin(x) {
    var upper = 50, i = 0, sum = 0, sign = true
    while i < upper {
        if sign {
            sum = sum + power(x, 1 + 2 * i) / factorial(1 + 2 * i)
        } else {
            sum = sum - power(x, 1 + 2 * i) / factorial(1 + 2 * i)
        }
        sign = !sign
        i = i + 1
    }
    return sum
}

func cos(x) {
    var upper = 50, i = 0, sum = 0, sign = true
    while i < upper {
        if sign {
            sum = sum + power(x, 2 * i) / factorial(2 * i)
        } else {
            sum = sum - power(x, 2 * i) / factorial(2 * i)
        }
        sign = !sign
        i = i + 1
    }
    return sum
}

func main() {
    print("input n: ")
    n = input()
    while n != "end" {
        n = number(n)
        print("sin(" + string(n) + ") = " + string(sin(n)) + "\n")
        print("cos(" + string(n) + ") = " + string(cos(n)) + "\n")
        print("input n: ")
        n = input()
    }
}

Pre-Release 0.1.0 of Lucy Lang

25 Feb 08:15
Compare
Choose a tag to compare
Pre-release
$ java -jar lucy.jar
usage: ljvm [options] target
 -c,--compile <file>    compile lucy source code to lucy bit code
 -d,--dump <file>       dump module object to human readable form
 -h,--help              print the help message and exit
 -o,--output <output>   output file path
 -r,--run <file>        run lucy X bit code
 -t,--token <file>      dump lucy tokens
 -v,--version           print the version information and exit
func fibonacci(n) {
    if n < 2 {
        return n
    } else {
        return fibonacci(n-1) + fibonacci(n-2)
    }
}

func main() {
    text = "input a number: "
    print(text)
    n = number(input())
    text = "The fibonacci number of " + string(n) + " is " + string(fibonacci(10))
    print(text)
}