A FRAMEWORK FOR DATA STRUCTURES IN A TYPED FORTH

Similar documents
CS Programming In C

Programming in C++ 4. The lexical basis of C++

Short Notes of CS201

At this time we have all the pieces necessary to allocate memory for an array dynamically. Following our example, we allocate N integers as follows:

[0569] p 0318 garbage

CS201 - Introduction to Programming Glossary By

Outline. Computer Memory Structure Addressing Concept Introduction to Pointer Pointer Manipulation Summary

Introduce C# as Object Oriented programming language. Explain, tokens,

Chapter 1 Getting Started

Advanced Systems Programming

Tokens, Expressions and Control Structures

Symbol Tables. ASU Textbook Chapter 7.6, 6.5 and 6.3. Tsan-sheng Hsu.

CS201- Introduction to Programming Current Quizzes

Syntax and Variables

Introduction to Programming Using Java (98-388)

Fachhochschule Wedel Technical Report Nr Implementing the Forth Inner Interpreter in High Level Forth

1. Describe History of C++? 2. What is Dev. C++? 3. Why Use Dev. C++ instead of C++ DOS IDE?

INTRODUCTION 1 AND REVIEW

CHAPTER 1 Introduction to Computers and Programming CHAPTER 2 Introduction to C++ ( Hexadecimal 0xF4 and Octal literals 031) cout Object

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

Basic Types, Variables, Literals, Constants

First of all, it is a variable, just like other variables you studied

CSE P 501 Compilers. Java Implementation JVMs, JITs &c Hal Perkins Winter /11/ Hal Perkins & UW CSE V-1


Programming Fundamentals. With C++ Variable Declaration, Evaluation and Assignment 1

Course Text. Course Description. Course Objectives. StraighterLine Introduction to Programming in C++

Chapter 1 GETTING STARTED. SYS-ED/ Computer Education Techniques, Inc.

C Programming. Course Outline. C Programming. Code: MBD101. Duration: 10 Hours. Prerequisites:

Binghamton University. CS-211 Fall Syntax. What the Compiler needs to understand your program

STUDY NOTES UNIT 1 - INTRODUCTION TO OBJECT ORIENTED PROGRAMMING

High Performance Computing in C and C++

Preface... (vii) CHAPTER 1 INTRODUCTION TO COMPUTERS

Final Exam. 11 May 2018, 120 minutes, 26 questions, 100 points

IT 1033: Fundamentals of Programming Data types & variables

A Fast Review of C Essentials Part I

C#: framework overview and in-the-small features

Object Oriented Programming. Assistant Lecture Omar Al Khayat 2 nd Year

Chapter 1 INTRODUCTION SYS-ED/ COMPUTER EDUCATION TECHNIQUES, INC.

UEE1302 (1102) F10: Introduction to Computers and Programming

Agenda. CSE P 501 Compilers. Java Implementation Overview. JVM Architecture. JVM Runtime Data Areas (1) JVM Data Types. CSE P 501 Su04 T-1

Introduction to C++ Introduction. Structure of a C++ Program. Structure of a C++ Program. C++ widely-used general-purpose programming language

Introduction to C++ with content from

Character Set. The character set of C represents alphabet, digit or any symbol used to represent information. Digits 0, 1, 2, 3, 9

Data Types. Every program uses data, either explicitly or implicitly to arrive at a result.

DEPARTMENT OF MATHS, MJ COLLEGE

Assist. Prof. Dr. Caner ÖZCAN

IMPORTANT QUESTIONS IN C FOR THE INTERVIEW

Programming in C++ 5. Integral data types

Data Types & Variables

UNIT- 3 Introduction to C++

Quiz Start Time: 09:34 PM Time Left 82 sec(s)

Basic program The following is a basic program in C++; Basic C++ Source Code Compiler Object Code Linker (with libraries) Executable

Name :. Roll No. :... Invigilator s Signature : INTRODUCTION TO PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70

CS201 Latest Solved MCQs

Final CSE 131B Spring 2004

QUIZ. 1. Explain the meaning of the angle brackets in the declaration of v below:

EL6483: Brief Overview of C Programming Language

CSCI 102L - Data Structures Midterm Exam #1 Fall 2011

Inclusion Polymorphism

CS 2461: Computer Architecture I

Programming in C - Part 2

CPS 506 Comparative Programming Languages. Programming Language

Introduction to C++ Systems Programming

Declaring Pointers. Declaration of pointers <type> *variable <type> *variable = initial-value Examples:

Review of the C Programming Language for Principles of Operating Systems

3/7/2018. Sometimes, Knowing Which Thing is Enough. ECE 220: Computer Systems & Programming. Often Want to Group Data Together Conceptually

Starting Savitch Chapter 10. A class is a data type whose variables are objects. Some pre-defined classes in C++ include int,

Operators in C. Staff Incharge: S.Sasirekha

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

CS250 Final Review Questions

QUIZ. What are 3 differences between C and C++ const variables?

CS250 Final Review Questions

Writing an ANSI C Program Getting Ready to Program A First Program Variables, Expressions, and Assignments Initialization The Use of #define and

Computer Science & Information Technology (CS) Rank under AIR 100. Examination Oriented Theory, Practice Set Key concepts, Analysis & Summary

Reminder: compiling & linking

CprE 288 Introduction to Embedded Systems Exam 1 Review. 1

Intermediate Code Generation

CS107 Handout 08 Spring 2007 April 9, 2007 The Ins and Outs of C Arrays

This manual is for Libffi, a portable foreign-function interface library. Copyright c 2008, 2010, 2011 Red Hat, Inc. Permission is granted to copy,

Pace University. Fundamental Concepts of CS121 1

VALLIAMMAI ENGINEERING COLLEGE

CS201 Some Important Definitions

C++ (Non for C Programmer) (BT307) 40 Hours

LESSON 1. A C program is constructed as a sequence of characters. Among the characters that can be used in a program are:

Data Storage. August 9, Indiana University. Geoffrey Brown, Bryce Himebaugh 2015 August 9, / 19

FOR Loop. FOR Loop has three parts:initialization,condition,increment. Syntax. for(initialization;condition;increment){ body;

Data Representation and Storage. Some definitions (in C)

CS103L PA4. March 25, 2018

COS 140: Foundations of Computer Science

calling a function - function-name(argument list); y = square ( z ); include parentheses even if parameter list is empty!

Object-Oriented Programming, Iouliia Skliarova

Chapter-8 DATA TYPES. Introduction. Variable:

Laboratory 2: Programming Basics and Variables. Lecture notes: 1. A quick review of hello_comment.c 2. Some useful information

Programming Logic and Design Sixth Edition

COMPUTER APPLICATION

CMPE-013/L. Introduction to C Programming

Scientific Programming in C VI. Common errors

For example, let s say we define an array of char of size six:

CS107 Handout 13 Spring 2008 April 18, 2008 Computer Architecture: Take II

C#.Net. Course Contents. Course contents VT BizTalk. No exam, but laborations

Transcription:

A FRAMEWORK FOR DATA STRUCTURES IN A TYPED FORTH Federico de Ceballos Universidad de Cantabria federico.ceballos@unican.es September, 2007

Strong Forth as a typed Forth In Strong Forth the interpreter knows the types associated with the elements in the stack. The compiler takes care that a word's behavior is consistent with its declared specification. This has to be unique. SWAP ( SINGLE SINGLE -- 2ND 1ST ) Because every word has a declared input diagram, overloading is possible. SWAP ( DOUBLE DOUBLE -- 2ND 1ST ) If you do need complete control, you can use a cast. CHAR 0. \ prints 0 CHAR 0 CAST SINGLE. \ prints 48

Strong Forth's type hierarchy The language comes with several families of types: SINGLE INTEGER UNSIGNED SIGNED CHARACTER LOGICAL FLAG TOKEN MEMORY-SPACE FILE DOUBLE INTEGER-DOUBLE UNSIGNED-DOUBLE SIGNED-DOUBLE CONTROL-FLOW DATA-TYPE STACK-DIAGRAM DEFINITION COLON-DEFINITION FLOAT

Strong Forth's type hierarchy (2) SINGLE ADDRESS DATA CONST PORT CODE CADDRESS SFADDRESS DFADDRESS CDATA CCONST CPORT CCODE SFDATA SFCONST SFCODE DFDATA DFCONST DFCODE DOUBLE FAR-ADDRESS CFAR-ADDRESS SFFAR-ADDRESS DFFAR-ADDRESS

Forth's advantages Usability. Interactive development. Efficiency (both in terms of development time and generated code). Possibility of accessing low-level resources. Little constraints. Intimate knowledge of your development environment. Typeless. (A language feature, not a bug.) ;-)

Variations from the Strong Forth model Strong Forth is aimed to embedded systems with multiple address spaces, we are targeting a PC with a single address space. Strong Forth tries to keep the Forth flavour as much as possible, we plan to take advantage of the new possibilities offered. The idea of having different base pointers in order to navigate through different data sizes is abandoned.

Data definitions in Forth VARIABLE FIRST 2VARIABLE SECOND FVARIABLE THIRD SFVARIABLE FOURTH DFVARIABLE FIFTH CREATE SIXTH 3 CELLS ALLOT 23 VALUE THIS CLASS POINT VARIABLE X VARIABLE Y END-CLASS POINT BUILDS MY-POINT

Data definitions in Forth 23 FIRST! SECOND 2@ 1.23E THIRD F! 1.23E FOURTH SF! 1.23E FIFTH DF! 1 2 3 SIXTH TUCK! CELL+ TUCK! CELL+! 45 TO THIS 12 MY-POINT X! 14 MY-POINT Y! 14 12 MY-POINT TUCK X! Y! \ An error!!!

Basic data types The Strong Forth system provides the following basic data types from which the rest are derived: SINGLE DOUBLE FLOAT A single-cell data type, used to hold a number or an address. A double-cell data type, used to hold a double number or else a couple of single-cell items working together. A data type wide enough to hold a floating point number. In a 16-bit system with 64-bit reals, a FLOAT would occupy four cells. In addition, we are using the following basic type: TRIPLE A data type three cells wide, used to hold three singlecells items working together.

User data types CHAR INT UINT LONG ULONG 1 BYTE 1 CELL 1 CELL (UNSIGNED) 2 CELLS 2 CELLS (UNSIGNED) REAL 1 FLOAT COMPLEX 2 FLOATS VECTOR 3 FLOATS QUATERNION 4 FLOATS

User data types C& D& C[] D[] C[,] D[,] An address to a read-only element. An address to an element (derived from the type above). An address to an array of read-only elements, together with the number of elements. An address to an array of elements, together with the number of elements (derived from the type above). An address to a two dimensional array of read-only elements, together with the number of rows and columns. An address to a two dimensional array of elements, together with the number of rows and columns (derived from the type above). C& points to an element that can be read. D& points to an element that can also be modified. D& is derived from C&, as you can do with a normal pointer everything you would do with a pointer to constant plus a few new things.

Categories All categories allocate enough memory to hold a value of the given type. VAR AUTO CONST No action associated with the word. When it is executed, its address is returned. When the word is executed, its address is returned and the @ word is applied to it. After the definition, the! word is applied to its address. When the word is executed, its constant address is returned and the @ word is applied to it. After the definition, the! word is applied to its address as if it were not constant. CONST words cannot be used inside a struct. AUTO words are not initialised inside a struct.

Manipulators A manipulator is a state smart word that fetches the next word in the input stream (that has to be an instance of one of the categories given above) and SIZEOF ADDR TO Returns the size of the object measured in address units. Returns the starting address of the object. Applies the word! to the object. Examples: +10 CONST INT 5*2 VAR INT FIRST -5 AUTO INT SECOND SIZEOF FIRST \ returns 4 in a 32-bit system +10 FIRST! FIRST @ \ returns +10 +10 TO SECOND SECOND \ returns +10 +10 ADDR SECOND! SECOND \ returns +10

One dimensional arrays A vector is a collection composed of a fixed number of elements of the same base type. Each of the elements can be used to hold a piece of data and the vector can be manipulated as a whole. 10 [] INT DISTANCES \ defines the vector SIZEOF DISTANCES DISTANCES. \ returns 40 in a 32-bit machine \ prints the value of all elements The following words should be provided for an array of any of the basic types: SIZE ( d[] -> type -- uint ) Returns the number of elements in the array. @ ( d[] -> type uint -- 2nd ) Fetches one of the elements in the array. An exception is thrown is the index is equal or greater than the number of elements in the array.! ( type d[] -> 1st uint ) Sets one of the elements in the array. An exception is thrown is the index is equal or greater than the number of elements in the array.

One dimensional arrays HEAD ( c[] -> type uint -- 1st ) Returns an array with some of the first entries in the original array. If the number is greater than the entries in the array, the whole array is returned. TAIL ( c[] -> type uint -- 1st ) Returns an array with some of the last entries in the original array. If the number is greater than the entries in the array, the whole array is returned. -HEAD( c[] -> type uint -- 1st ) Returns an array in which some of the first entries in the original array have been removed. If the number is greater than the entries in the array, an empty array is returned. -TAIL( c[] -> type uint -- 1st ) Returns an array in which some of the last entries in the original array have been removed. If the number is greater than the entries in the array, an empty array is returned. CLONE( c[] -> type -- d[] -> 2nd ) Returns a new array with the same data as the given one.

Two dimensional arrays The following words are modelled after their one dimensional counterparts: SIZE ( d[,] -> type -- uint uint ) @ ( d[,] -> type uint uint -- 2nd )! ( type d[,] -> 1st uint uint ) HEAD ( d[,] -> type uint -- 1st ) TAIL ( d[,] -> type uint -- 1st ) -HEAD( d[,] -> type uint -- 1st ) -TAIL( d[,] -> type uint -- 1st ) CLONE( d[,] -> type -- 1st ) In addition, we have the following word: ROW ( d[,] -> type uint -- d[] -> 2nd1 ) Returns an array with one row of the original array.

Strings A string can be defined as a one dimensional array of characters as in: 50 [] CHAR ADDRESS However, as the Forth language allows for direct use of strings, some convenience can be provided. The keyword STRING defines a constant array of chars with the length of its initialiser: " This is some text" STRING MY-TEXT

Structures STRUCT POINT-2D VAR INT X VAR INT Y END POINT-2D A basic type with an associated size of 8 bytes (in a 32 bit machine). X ( C& -> POINT-2D -- 1ST -> INT) The returned address is the same as the parameter. Y ( C& -> POINT-2D -- 1ST -> INT) The returned address is the parameter incremented in 4 bytes.

Using structures STRUCT DATE VAR UINT DAY VAR UINT MONTH VAR UINT YEAR END :! ( UINT UINT UINT D& -> DATE ) >R R@ YEAR! R@ MONTH! R> DAY! ; : @ ( C& -> DATE -- UINT UINT UINT ) >R R@ DAY @ R@ MONTH @ R> YEAR @ ; : 00 ( UINT ) <# # # #> TYPE ; :. ( C& -> DATE ) @ ROT 00 '/' EMIT SWAP 00 '/' EMIT. ; 15 9 2007 CONST DATE TODAY TODAY MONTH. \ prints 9

Derived structures A new structure can be a subtype of another type. In this case, the newer one has the size of the old one (augmented with the size of new components) and can use any word that apply to the old one (unless a new overloaded one is defined). STRUCT POINT-2D VAR INT X VAR INT Y END DERIVED POINT-2D POINT-3D VAR INT Z END :. ( C& -> POINT-2D ) DUP X. Y. ; :. ( C& -> POINT-3D ) DUP. Z. ;

Privacy matters In an OOP language, the following concepts may be available: PRIVATE PROTECTED PUBLIC FRIEND A method that can be used only inside the class. A method than can be used inside the class and also inside derived classes. A method without privacy constraints. An external method that can access all method in the class. In Forth, the use of word lists or packages caters for all these possibilities and then some more.

Future lines of work The distinction between normal manipulators (that increment the data pointer) and manipulators inside a structure (that increment the structure size) can be complemented with USER manipulators (the increment the offset into the user data space). USER VAR INT UNO USER AUTO LONG DOS If it were possible to derive structures from a TAGGED one, the tab could be used at runtime to choose the exact function that should be called. This modification is not trivial. TAGGED FIRST VAR INT X END DERIVED FIRST SECOND VAR INT Y END