Sunday, September 6, 2020

A quick look at Wren.

Wren - A quick introduction

Introduction

Welcome to my quick introduction to the Wren Programming language.
In this post i will be covering the basics of this interesting little language.
I hope by the end you have not only learned a little about Wren, but you have had your interested piqued by it like mine has been.


What is Wren

  • Wren bills itself as a "Small,fast,classed-based concurrent scripting language."
  • It is a small embeddable scripting language written entirely in C99.
  • It has syntax which is familiar to C.
  • Wren is based abound classes. OOP here we come.

Variables

Variables are one of the most basic parts of a programming language.
In wren, unlike C, you don't need to declare the type of the variable.
Variables are declared using the var keyword.

  • var name = value declares a variable name containing value
  • var can hold int,floats,bools,strings,lists,objects,functions,etc.
  • vars can then be reassigned from one type to another.
declaring and using variables in Wren.Find the code here.

Lists

Creating and using lists in wren is very simple.
List are dynamic, meaning they have no fixed size.
They can also hold a mixed of different types at once.
Lists can also be used as a map or dictionary.

  • lists start at 0.
  • lists can hold many different types at one.
  • negative index values start at the end of the list and go backwards.
Lists in wren.Find the code here.

Lists can also be used as a map/dictionary.
Keys can be almost anything:strings,variables,numbers,objects,etc.
values can also be almost anything.

Maps in Wren.Find the code here.

Control flow

Control flow in wren is very similar to C.
if statements are near identical.
wren uses the same set of operators such as == and <
The only difference with C you need to watch out for is 0 is evaluated as true.

Control in wren.Find the code here.

while loops in wren look identical to C while loops. Nothing to explain here
for loops are a bit different. You loops over a range or over a list.
Lists can be created in the loop or you can use a list which was created prior to the loop.
To loop through a map you use mapname.keys or mapname.values.

loops in wren.Find the code here.

Classes and Objects

Wren is based around classes. But luckily wren makes classes very easy.
Anyone familiar with most OOP languages will get this stuff very easily.
Even without an OOP background it is fairly easy to get.

  • class name {}
    • make a new class called name.
  • construct new(args)
    • constructor for the class.
  • _fieldname
    • declare a field for the class named fieldname
  • methodName=(value) {})
    • declare methodName a setter for the class.
  • methodName {_fieldname}
    • declare methodName a getter for class.
  • class childclass in parentclass
    • declare childclass to be a child class of parentclass.
  • super
    • calls the method or field in the parent class.
classes in wren.Get the code here.

Final remarks

Thank you for checking out my introduction to Wren. I hope you find the language as interesting as I do.
I will be covering embedding wren into C in a separate blog poster later on. Hope to see you there.
For those looking to make games, there is a Game engine using wren.
I also suggest you check out the wren wiki for more info,libraries,projects,and applications.

No comments:

Post a Comment