DEV Community

Ingi
Ingi

Posted on

Plang programming language Lessons - Basics

This article explores Plang, an intent-based programming language designed to interpret natural language. For more information, visit plang.is

We will start by learning how to structure your code. This is a natural language programming language, but you have to follow some rules.

First are the files & folders. There are a few important ones:

  • Start.goal - This is the default entry point into a Plang app.
  • Setup.goal - This is where you set up the system, create tables, and insert config data. Each step only runs once in the lifetime of your application.
  • Events folder - You can bind events to goals and steps.
  • .build folder - Where your code is compiled to.
  • .db folder - Contains the database.

Those are the important ones to learn first.

Goal

A goal is something you want to accomplish, similar to a function/method in other languages.

GetProductInfo is a goal. Getting product information involves multiple steps, such as retrieving the data from a database and then displaying the data.

Steps

  • Each goal has one or more steps.
  • Each step starts with a dash (-).
  • Each step defines the intent of the developer, e.g.

Step example:

- read file.txt into %content%
Enter fullscreen mode Exit fullscreen mode

I feel I don't have to explain what this code does, do I?

Just in case, the developer wants the app to read the file.txt and put the text into the variable %content%.

Variables

Variables are defined with starting and ending %. Here are examples of the %name%, %users%, %userInfo% variables.

Variable examples:

- set %name% = "jonny"
- select * from users, write to %users%
- get https://jsonplaceholder.typicode.com/users/1, %userInfo%
Enter fullscreen mode Exit fullscreen mode

Now you can use those variables:

- write out 'Hello %name%'
- write out 'There are %users.Count%'
- write out 'The user email is %userInfo.email%'
Enter fullscreen mode Exit fullscreen mode

Advanced: The underlying runtime is C#, so you can use Properties and Methods from the C# API. In the above example, I used %users.Count%. Count is a property on the List class.

%Now%

Current time is always important. You can access it like this: %Now% or %NowUtc%. All the properties and methods are available.

You can also say %Now+1day%, %Now+1hour%, %Now+1ms%. Read more about Time.

Goal File Structure

Give the goal file a good name that defines the goal you want to achieve.

The file always starts with the name of the goal:

ReadFile
Enter fullscreen mode Exit fullscreen mode

It should be the same as the name of the file minus the '.goal'.

Then come the steps, what do you need to do to accomplish this goal?

Each step starts with a dash (-). It can be multiple lines, but new lines cannot start with a dash (-).

ReadFile
- read file.txt, into %content%
- write out %content%
Enter fullscreen mode Exit fullscreen mode

Those are the steps in the goal ReadFile. They are easy to understand.

Now you know how Plang is structured: goal, steps, and what a variable is.

What is next?

If this has caught your interest, you should set up Plang on your computer and write your first app that uses API and database

Top comments (0)