Der deutsche Education Blog

October, 2008

Blog - Title

October, 2008

  • Small Basic

    Extending Small Basic

    • 18 Comments

    The Small Basic compiler is designed to allow external libraries to be plugged in that enable it to be extended in interesting ways.  These libraries can be built using any .Net based language and compiled to a .Net assembly.  There are a few rules that the Small Basic compiler expects for a type to be identified as a Small Basic “object.” 

    1. The Type should be declared static
    2. The Type should be adorned with SmallBasicTypeAttribute
    3. Properties should be of type Microsoft.SmallBasic.Library.Primitive
    4. All the input and output parameters for Methods should be of type Microsoft.SmallBasic.Library.Primitive
    5. All events should be of type Microsoft.SmallBasic.Library.SmallBasicCallback

    Once these conditions are met, you can compile your assembly and put it in a folder named “lib” in the Small Basic’s install location.  For example, if Small Basic was installed on your “c:” drive and your library was called “myextensions”, you’d have to put myextensions.dll inside “c:\program files\microsoft\small basic\lib” folder.

    Optionally, you can enable XML documentation in your build and copy over the Doc Xml file along with the library.  This will automatically enable the help text inside Intellisense and the context help pane.

    Here’s a sample extension (written in C#) that exposes a Settings object to Small Basic and lets you store and retrieve name value pairs specific to a program.

    using System.Collections.Generic;
    using System.IO;
    using System.Reflection;
    using System.Runtime.Serialization.Formatters;
    using System.Runtime.Serialization.Formatters.Binary;
    using Microsoft.SmallBasic.Library;
     
    namespace MyExtensions
    {
        /// <summary>
        /// The Settings library consists of helpers that allow programs to
        /// store and retrieve user settings.
        /// </summary>
        [SmallBasicType]
        public static class Settings
        {
            static Primitive _filePath = new Primitive();
     
            /// <summary>
            /// Gets the file path for the settings file.
            /// </summary>
            public static Primitive FilePath
            {
                get
                {
                    if (string.IsNullOrEmpty(_filePath))
                    {
                        _filePath = Path.ChangeExtension(
                                        Assembly.GetEntryAssembly().Location,
                                        ".settings");
                    }
     
                    return _filePath;
                }
            }
     
            /// <summary>
            /// Gets the value for the setting identified by the specified name.
            /// </summary>
            /// <param name="name">
            /// The Name of the setting.
            /// </param>
            /// <returns>
            /// The Value of the setting.
            /// </returns>
            public static Primitive GetValue(Primitive name)
            {
                if (System.IO.File.Exists(FilePath))
                {
                    using (Stream stream = System.IO.File.Open(FilePath,
                                                               FileMode.Open))
                    {
                        Dictionary<string, string> contents = ReadContents(stream);
                        if (contents.ContainsKey  (name)) { return contents[name]; }
                    }
                }
     
                return "";
            }
     
            /// <summary>
            /// Sets a value for a setting identified by the specified name.
            /// </summary>
            /// <param name="name">
            /// The Name of the setting.
            /// </param>
            /// <param name="value">
            /// The Value of the setting.
            /// </param>
            public static void SetValue(Primitive name, Primitive value)
            {
                Dictionary<string, string> contents = null;
                if (System.IO.File.Exists(FilePath))
                {
                    using (Stream stream = System.IO.File.Open(FilePath,
                                                               FileMode.Open))
                    {
                        contents = ReadContents(stream);
                    }
                }
                else
                {
                    contents = new Dictionary<string, string>();
                }
     
                contents[name] = value;
                using (Stream stream = System.IO.File.Open(FilePath,
                                                           FileMode.Create))
                {
                    WriteContents(stream, contents);
                }
            }
     
            static Dictionary<string, string> ReadContents(Stream stream)
            {
                BinaryFormatter formatter = new BinaryFormatter();
                formatter.AssemblyFormat = FormatterAssemblyStyle.Simple;
                return (Dictionary<string, string>)formatter.Deserialize(stream);
            }
     
            static void WriteContents(Stream stream, Dictionary<string, string> map)
            {
                BinaryFormatter formatter = new BinaryFormatter();
                formatter.AssemblyFormat = FormatterAssemblyStyle.Simple;
                formatter.Serialize(stream, map);
            }
        }
    }

     

    Share your extensions in our forum.

  • Small Basic

    Hello World

    • 48 Comments

    Welcome to Small Basic Blogs!  After being in part-time development for nearly a year, Small Basic is finally out, and I'm excited to see where this will go from here!

    Screenshot

    History 

    It all happened in August of last year when someone sent me a pointer to the article Why Johnny Can't Code and it got me thinking.  After all, when I was a kid, I started programming in ZX Spectrum with a built in Sinclair BASIC interpreter and did so until I ran into Turbo BASIC.  To me that transformation was groundbreaking and was the single most important reason why I chose to write software for a living, for the rest of my life.

    An informal poll along the corridors in Microsoft revealed that most developers within Microsoft had started programming in some variant of BASIC.  It had all the good characteristics of a good beginner programming language - simplicity, minimal ceremony, instant gratification and ubiquity.  It helped them "get" programming and assisted them with understanding the need for more advanced concepts.

    When I asked them how they're going to teach programming to their children, they were stumped.  Almost everyone wanted to, they just didn't know how.  Some said KPL, Python and Ruby.  Some said Alice and Scratch.  But they all felt that none of these have the charm of BASIC.  Of course there were some that took the Dijkstra's stand, but they were few.

    Of the numerous programming languages, BASIC, from its inception in the 1960s has undergone some major transformations.  Even among Microsoft's BASIC offerings, the language and the environment (VS) has been repeatedly updated to include more powerful features with every release.  On the one hand this makes the language and the environment very powerful and capable, but on the other hand, it makes it daunting for a beginner. 

    That got me thinking as to why isn't there a "Small" variant of BASIC that brings the simplicity of the original language to the modern day.  And after a year, here we are, announcing Small Basic.  Small Basic is a project that will help make programming easy and approachable for beginners.  Now, that's a pretty big claim - let's see how Small Basic does it.

    Make programming approachable

    Small Basic starts with a really simple programming language that gathers inspiration from the original BASIC language.  It has no more than 15 keywords and is strictly imperative.  There are no classes, scopes, generics, lambdas, etc. - just pure imperative code.  The language is typeless and all variables are dynamic and global all the time.  The code gets compiled to IL and runs on the .Net Framework.

    It comes with a set of libraries that can be accessed from within a Small Basic program.  Since the language itself is .Net based, new libraries can be created or the existing libraries modified using any .Net programming language. 

    Next, it combines the features of the language and the libraries into a very simple and friendly programming environment.  This environment gives beginners, access to professional features like Intellisense(TM) and Instant context sensitive help.  It makes writing programs and executing them a breeze.

    Show me code already

    Okay here're a couple sample Small Basic programs

    Sample 1: Change the desktop wallpaper  from Flickr, every minute

    While ("True")
      pic = Flickr.GetRandomPicture("fall leaves")
      Desktop.SetWallPaper(pic)
      
      Program.Delay(60 * 1000)
    EndWhile
    

    Sample 2: Makes a BlackBoard that allows you to scribble on a window

    GraphicsWindow.BackgroundColor = "Black"
    GraphicsWindow.PenColor = "White"
    GraphicsWindow.MouseDown = OnMouseDown
    GraphicsWindow.MouseMove = OnMouseMove
    
    Sub OnMouseDown
      prevX = GraphicsWindow.MouseX
      prevY = GraphicsWindow.MouseY
    EndSub
    
    Sub OnMouseMove
      x = GraphicsWindow.MouseX
      y = GraphicsWindow.MouseY
      If (Mouse.IsLeftButtonDown) then
        GraphicsWindow.DrawLine(prevX, prevY, x, y)
      endif
      prevX = x
      prevY = y
    EndSub
    

     

    How can I get this?

    You can download Small Basic today by visiting the Small Basic portal.

    Also, don't forget to check out the Getting Started Guide.

    Happy Programming!

Page 1 of 1 (2 items)