Git Product home page Git Product logo

extended-js-subset's Introduction

Code Coverage

Package Line Rate Health
Interpreter.Lib 40%
Interpreter 100%
Summary 43% (925 / 2173)

Minimum allowed line rate is 80%

"Расширенное подмножество ЯП JavaScript"

Вводная информация

За основу был взят стандарт ECMA-262

Лексическая структура

Грамматика

Рабочие примеры

Цель проекта

Реализовать JavaScript с объектами и статической структурной типизацией, избавившись от таких понятий, как constructor, class, interface

Конструкции языка

Типизация

В языке структурная статическая сильная типизация.

Есть 5 примитивных типов:

  1. number
  2. boolean
  3. string
  4. null
  5. void

Остальные типы делятся на группы:

  • NullableType (тип, который допускает значение null)
  • ObjectType (тип объекта, является NullableType)
  • ArrayType (списковый тип)
  • FunctionType (тип функции)
Значения по умолчанию
Тип Значение
number 0
boolean false
string ""
NullableType null
ArrayType []
type alias

Можно создать свой type alias по типу того, как это сделано в С++

type int = number
type maybeInt = int?
type ints = int[]
type point = {
    x: int;
    y: int;
}
type handleInts = (ints) => void
type handler = {
    items: ints;
    handle: handleInts;
}

Объявление переменных

let i = 1 // интерпретатор выведет тип из выражения
let j: number // запишет значение по умолчанию в переменную
let k: number = 1 // полностью явное объявление

Объекты

let v2d = {
    // обычное поле
    x: 3;
    y: 4;
    //метод
    lengthSquared => () {
        // в методе доступны поля объекта
        // и указатель this
        return x * x + y * y
    };
}

Списки

let array = [1, 2, 3]
let size = ~array // длина списка
array::1 // удаление элемента по индексу
array = array ++ [5, 7] // конкатенация списков

Операторы

Оператор Вид Типы операндов Тип операции
+ бинарный оба number, оба string number, string
*, -, /, % бинарный number number
||, && бинарный boolean boolean
!=, == бинарный равный с двух сторон boolean
<=, >=, >, < бинарный number boolean
! унарный boolean boolean
- унарный number number
++ бинарный [] []
:: бинарный [] и number void
~ унарный [] number

Ветвление

if (1 == 1) {
    // ...
} else if (2 == 2) {
    // ...
}
else {
    // ...
}
// в общем как в Си подобных языках
// главное, чтобы выражение условия
// возвращало boolean

Также есть тернарный оператор

let x = 1 > 0 ? 0 <= 1 ? 1 : 0 : -2 < 0 ? -1 : 0

Цикл

while (cond) {
    // ...
    continue
    // ...
    break
}

Функции

// объявление
function add(a: number, b: number): number {
    return a + b
}
// вызов
let c = add(1, 2)

Операции доступа

// объекты
let x = v2d.x
let s = v2d.lengthSquared()
// массивы
let l = array[2]

Приведение типов

let s = v2d as string

Стандартная библиотека

  • Функция print c сигнатурой (string) => void; осуществляет печать строки на экран

Требования

  • .NET 7 SDK

Сборка

После клонирования репозитория идём в папку проекта Interpreter.

Там выполняем команду: dotnet publish -r <RUNTIME_IDENTIFIER> -p:PublishSingleFile=true -p:DebugType=embedded --self-contained false -o <OUTPUT_DIRECTORY>

Список идентификаторов рантайма лежит тут

Запуск

Interpreter 1.2.6
Copyright (C) 2022 Interpreter
USAGE:
Simple interpretation call:
  Interpreter file.js
Request dump:
  Interpreter --dump file.js

  -d, --dump                (Default: false) Show dump data of interpreter

  --help                    Display this help screen.

  --version                 Display version information.

  InputFilePath (pos. 0)    Required. Path to input file

Источники:

  1. ECMA-262
  2. DragonBook
  3. Stanford CS143 Lectures

extended-js-subset's People

Contributors

stepami avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.