Skip to content
Izmaylov Alexandr edited this page Jan 22, 2018 · 9 revisions

Welcome to the BobScript wiki!

Справка

  1. Циклы
  2. Массивы

ключевые слова

if

while

end

func

return

for

array

else

elif

break

continue

примеры

Hello, World!

print('Hello, world!')

Пример ввода

cnt = parseInt(readLine())
for (i = 0; i < cnt; i += 1)
  print('*')
  print(i)
end

Факториал

func fac(n)
  if n == 0
    return 1
  end
  return fac(n - 1) * n
end

print('Enter the number')
n = parseInt(readLine())
print(fac(n))

Добавление методов к объекту

math = 'math object'

// add frac to math
func math.frac(n)
    if n == 0
        return 1
    else
        return this.frac(n - 1) * n
    end
end

// add gcd to math
func math.gcd(a, b)
    if b == 0
        return a
    else
        return this.gcd(b, a % b)
    end
end

// add pi to math
func math.pi()
    return 3.14159265359
end

// add circleArea to math
func math.circleArea(r)
    return r * r * this.pi()
end

// add pow to math
func math.pow(a, b)
    if b == 0
        return 1
    end
    tmp = this.pow(a, b / 2)
    tmp = tmp * tmp
    if b % 2 == 1
        tmp = tmp * a
    end
    return tmp
end

// add pow2 to math
func math.pow2(a)
    f = |item| -> this.pow(2, item)
    return f(a)
end

// check
println(math.frac(5))
println(math.gcd(10, 15))
println(math.pi())
println(math.circleArea(2.0))
println(math.pow(2, 10))
println(math.pow2(9))
println(math)

Связный список

class LinkedListNode
    field next
    field prev
    field value

    func new(value)
        this.value = value
        this.next = null
        this.prev = null
    end

    func setNext(next)
        this.next = next
    end

    func setPrev(prev)
        this.prev = prev
    end

    func toStr()
        if this.next == null
            return this.value.toStr()
        else
            return this.value + ', ' + this.next.toStr()
        end
    end
end

class LinkedList
    field root
    field last

    func new()
        this.root = null
        this.last = null
    end

    func add(value)
        if this.root == null
            this.root = new LinkedListNode(value)
            this.last = this.root
        else
            next = new LinkedListNode(value)
            this.last.setNext(next)
            next.setPrev(this.last)
            this.last = this.last.next
        end
    end

    func remove(value)
        current = this.root
        while (true)
            if current == null
                break
            end

            if current.value == value
                current.prev.setNext(current.next)
                current.next.setPrev(current.prev)
                break
            end

            current = current.next
        end
    end

    func get(index)
        current = this.root
        for (i = 0; i < index; ++i)
            if current == null
                return null
            end
            current = current.next
        end
        if current == null
            return null
        else
            return current.value
        end
    end

    func toStr()
        if this.root == null
            return '[]'
        else
            return '[' + this.root.toStr() + ']'
        end
    end

end

// test
list = new LinkedList()
print(list)
for (i = 0; i < 10; ++i)
    list.add(i)
end
list.remove(4)
print(list + ' ' + list.get(3))

Foreach loop, iterator, file reading

open('file.txt', 'r', |file| -> do
    print('read file!')
    for line in file.lines()
        print(line + ' ' + line.length())
    end
end)

class MyIterator
    field value, step, e
    field current

    func new(value, step, e)
        this.value = value
        this.step = step
        this.e = e
        this.current = this.value - this.step
    end

    func iterator() this

    func next?() this.current + this.step < this.e

    func next()
        this.current += this.step
        return this.current
    end
end

for i in (new MyIterator(1, 3, 20)) print(i)