项目作者: i2y

项目描述 :
Jet is a simple OOP, dynamically typed, functional language that runs on the Erlang virtual machine (BEAM). Jet's syntax is Ruby-like syntax.
高级语言: Elixir
项目地址: git://github.com/i2y/jet.git
创建时间: 2016-04-19T03:59:11Z
项目社区:https://github.com/i2y/jet

开源协议:MIT License

下载


“I thought of objects being like biological cells and/or individual
computers on a network, only able to communicate with messages”
—Alan Kay, creator of Smalltalk, on the meaning of “object oriented programming”

Jet is a simple OOP, dynamically typed, functional language that runs on the Erlang virtual machine (BEAM).
Jet’s syntax is Ruby-like syntax.
Jet was inspired by Reia and Celluloid.

Language features

Builtin Types

  1. ### Numbers
  2. 49 # integer
  3. 4.9 # float
  4. ### Booleans
  5. true
  6. false
  7. ### Atoms
  8. :foo
  9. ### Lists
  10. list = [2, 3, 4]
  11. list2 = [1, *list] # => [1, 2, 3, 4]
  12. [1, 2, 3, *rest] = list2
  13. rest # => [4]
  14. list.append(5) # => [2, 3, 4, 5]
  15. list # => [2, 3, 4]
  16. list.select {|item| item > 2}
  17. .map {|item| item * 2} # => [6, 8]
  18. list # => [2, 3, 4]
  19. # list comprehensions
  20. [n * 2 for n in list] # => [4, 6, 8]
  21. ### Tuples
  22. tuple = {1, 2, 3}
  23. tuple.select {|item| item > 1}
  24. .map {|item| item * 2} # => [4, 6]
  25. tuple.to_list # => [1, 2, 3]
  26. ### Maps
  27. dict = {foo: 1, bar: 2}
  28. dict2 = dict.put(:baz, 3) # => {foo: 1, bar: 2, baz: 3}
  29. dict # => {foo: 1, bar: 2}
  30. dict.get(:baz, 100) # => 100
  31. ### Strings (Lists)
  32. "Abc"
  33. ### Anonymous functions (Blocks)
  34. add = {|x, y| x + y}
  35. add(40, 9) # => 49
  36. multiply = do |x, y|
  37. x * y
  38. end
  39. multiply(7, 7) # => 49
  40. ### Binaries
  41. <<1, 2, 3>>
  42. <<"abc">>
  43. <<1 , 2, x>> = <<1, 2, 3>>
  44. x # => 3

Class definition

Car.jet

  1. Module Car
  2. class Car
  3. # On jet, state of an instance is immutable.
  4. # The initialize method returns initial state of an instance.
  5. def initialize()
  6. {name: "foo",
  7. speed: 100}
  8. end
  9. def display()
  10. @name.display()
  11. @speed.display()
  12. end
  13. end
  14. end

Module definition

Enumerable.jet

  1. module Enumerable
  2. def select(func)
  3. reduce([]) {|item, acc|
  4. if func.(item)
  5. acc ++ [item]
  6. else
  7. acc
  8. end
  9. }
  10. end
  11. def filter(func)
  12. reduce([]) {|item, acc|
  13. if func.(item)
  14. acc ++ [item]
  15. else
  16. acc
  17. end
  18. }
  19. end
  20. def reject(func)
  21. reduce([]) {|item, acc|
  22. if func.(item)
  23. acc
  24. else
  25. acc ++ [item]
  26. end
  27. }
  28. end
  29. def map(func)
  30. reduce([]) {|item, acc|
  31. acc ++ [func.(item)]
  32. }
  33. end
  34. def collect(func)
  35. reduce([]) {|item, acc|
  36. acc ++ [func.(item)]
  37. }
  38. end
  39. def min(func)
  40. reduce(:infinity) {|item, acc|
  41. match func.(acc, item)
  42. case -1
  43. 0
  44. case 0
  45. 0
  46. case 1
  47. item
  48. end
  49. }
  50. end
  51. def min()
  52. reduce(:infinity) {|item, acc|
  53. if acc <= item
  54. acc
  55. else
  56. item
  57. end
  58. }
  59. end
  60. def unique()
  61. reduce([]) {|item, acc|
  62. if acc.index_of(item)
  63. acc
  64. else
  65. acc ++ [item]
  66. end
  67. }
  68. end
  69. def each(func)
  70. reduce([]) {|item, acc|
  71. func.(item)
  72. }
  73. end
  74. end

Mixing in Modules

SampleList.jet

  1. module SampleList
  2. class SampleList
  3. include Enumerable
  4. def initialize(items)
  5. {items: items}
  6. end
  7. def reduce(acc, func)
  8. lists::foldl(func, acc, @items)
  9. end
  10. end
  11. end

Trailing closures (Trailing blocks)

  1. sample_list = SampleList::SampleList.new([1, 2, 3])
  2. sample_list.select {|item| item > 1}
  3. .map {|item| item * 2}
  4. # => [4, 6]

Other supported features

  • Tail recursion optimization
  • Pattern matching

Currently unsupported features

  • Class inheritance
  • Macro definition

Requirements

  • Erlang/OTP >= 18.0
  • Elixir >= 1.1

Installation

  1. $ git clone https://github.com/i2y/jet.git
  2. $ cd jet
  3. $ mix archive.build
  4. $ mix archive.install
  5. $ mix escript.build
  6. $ cp jet <any path>

Usage

Command

Compiling:

  1. $ ls
  2. Foo.jet
  3. $ jet Foo.jet
  4. $ ls
  5. Foo.beam Foo.jet

Compiling and Executing:

  1. $ cat Foo.jet
  2. module Foo
  3. def self.bar()
  4. 123.display()
  5. end
  6. end
  7. $ jet -r Foo::bar Foo.jet
  8. 123

Mix

mix.exs file example:

  1. defmodule MyApp.Mixfile do
  2. use Mix.Project
  3. def project do
  4. [app: :my_app,
  5. version: "1.0.0",
  6. compilers: [:jet|Mix.compilers],
  7. deps: [{:jet, git: "https://github.com/i2y/jet.git"}]]
  8. end
  9. end

“.jet” files in source directory(src) is automatically compiled by mix command.